This code is really good, exactly what I was looking for. You just need to install the AutoIT and add it to your project reference. Here is the vb.net version.
Also if you need to call it from a different module:
Sub ClickCertificate()
Dim cls as new CertificateDialogHandler
cls.ChooseCertificate("your certificate friendly name", "Windows Security")
'string "Windows Security" works on Win 7
'string "your certificate friendly name" needs to be changed to whatever you see on the certificate selection window
End sub
Imports System.Threading
Imports System.Text.RegularExpressions
Imports AutoITX3Lib
Public Class CertificateDialogHandler
Private autoIt As AutoItX3 = New AutoItX3
Public Sub ChooseCertificate(ByVal CertificateName As String, ByVal windowTitle As String)
Thread.Sleep(2)
Me.autoIt.WinWaitActive(windowTitle, "", 0)
Dim CurrentCertificate As String = ""
Me.ViewCertificateInfo(windowTitle)
CurrentCertificate = Me.GetCertificateIssuedTo
While True
If Not CurrentCertificate.Equals(CertificateName) Then
Me.SelectNextCertificate(windowTitle)
Me.ViewCertificateInfo(windowTitle)
Dim newCert As String = Me.GetCertificateIssuedTo
If newCert.Equals(CurrentCertificate) Then
Throw New SystemException("Certificate was not found!")
Else
CurrentCertificate = newCert
'TODO: Warning!!! continue Else
End If
Else
Me.PressBtnOK(windowTitle)
Exit While
End If
End While
End Sub
Public Sub ConfirmSecurtyException()
Thread.Sleep(2)
Me.autoIt.WinWaitActive("Security Alert", "", 0)
Me.PressBtnYes()
End Sub
Public Sub CancelCertificateDialog(ByVal windowTitle As String)
Thread.Sleep(2)
Me.autoIt.WinWaitActive(windowTitle, "", 0)
Me.PressBtnCancel()
End Sub
''' <summary>
''' Open certificate details window
''' </summary>
''' <param name="windowTitle">Title of certificate dialog</param>
Private Sub ViewCertificateInfo(ByVal windowTitle As String)
If windowTitle.Equals("Choose a digital certificate") Then
Me.autoIt.Send("{TAB}", 0)
Thread.Sleep(1000)
End If
Me.autoIt.Send("{SPACE}", 0)
Thread.Sleep(1000)
Me.autoIt.WinWaitActive("Certificate", "", 0)
End Sub
'''
''' Extract the certificate name from a "Certificate" window text
'''
''' The certificate name
Private Function GetCertificateIssuedTo() As String
Dim CertificateWndText As String
Dim sPattern As String
Dim SplittedCertificateWndText() As String
CertificateWndText = Me.autoIt.WinGetText("Certificate", "")
sPattern = "General" & vbLf & vbLf & "(.+?)" & vbLf
SplittedCertificateWndText = Regex.Split(CertificateWndText, sPattern)
Return SplittedCertificateWndText(1)
End Function
'''
''' Determines whether the certificate name is founded
''' in the "Certificate" window text
'''
'''
Private Function IsFoundedCertificate(ByVal CertificateName As String) As Boolean
Return (Me.GetCertificateIssuedTo = CertificateName)
End Function
'''
''' Return to the "Choose a digital certificate" window
''' and press the button "OK" when the certificate is founded
Private Sub PressBtnOK(ByVal windowTitle As String)
If Not windowTitle.Equals("Choose a digital certificate") Then
Me.autoIt.WinWaitActive("Certificate Details", "", 0)
End If
Thread.Sleep(1000)
Me.autoIt.Send("{ESC}", 0)
Thread.Sleep(1000)
Me.autoIt.Send("{TAB}", 0)
Thread.Sleep(1000)
Me.autoIt.Send("{ENTER}", 0)
End Sub
Private Sub PressBtnCancel()
'Thread.Sleep(1000);
'AutoIt.Send("{ESC}", 0);
Dim i As Integer = 0
Do While (i < 3)
Me.autoIt.Send("{TAB}", 0)
Thread.Sleep(1000)
i = (i + 1)
Loop
Me.autoIt.Send("{SPACE}", 0)
Thread.Sleep(1000)
End Sub
Private Sub PressBtnYes()
'Thread.Sleep(1000);
'AutoIt.Send("{ESC}", 0);
Dim i As Integer = 0
Do While (i < 2)
Me.autoIt.Send("{TAB}", 0)
Thread.Sleep(1000)
i = (i + 1)
Loop
Me.autoIt.Send("{SPACE}", 0)
Thread.Sleep(1000)
End Sub
'''
''' Return to the "Choose a digital certificate" window and
''' select next certificate name from the name list
'''
Private Sub SelectNextCertificate(ByVal windowTitle As String)
If Not windowTitle.Equals("Choose a digital certificate") Then
Me.autoIt.WinWaitActive("Certificate Details", "", 0)
End If
Thread.Sleep(1000)
Me.autoIt.Send("{ESC}", 0)
Thread.Sleep(1000)
Me.autoIt.Send("{TAB}", 0)
Thread.Sleep(1000)
Me.autoIt.Send("{DOWN}", 0)
End Sub
End Class