onsdag, mai 10, 2006

How to start a process with a different username in vb.net

Function ConvertToSecureString(ByVal str As String)

Dim password As New SecureString
For Each c As Char In str.ToCharArray

password.AppendChar(c)

Next

Return password

End Function

Sub Main()

dim username as string = "Administrator"

dim password as SecureString = ConvertToSecureString("my password")

dim domain as string = Nothing

dim filename as string = "notepad.exe" ' %SYSTEMROOT%\system32

Try

System.Diagnostics.Process.Start(filename,username, password, domain)

Catch ex As Win32Exception

MessageBox.Show("Wrong username or password.", "Error logging in as administrator", MessageBoxButtons.OK, MessageBoxIcon.Error)

End Try

End Sub