Process.Start code not working when trying to launch as a specified user

I found this code that should run a program as a different user, but it errors and doesn't run the program.  Is there something I'm not doing correctly?
Imports System.Security
Imports System.ComponentModel

Module Module1

    Sub Main()
        Dim username As String = "MyUser"
        Dim password As SecureString = ConvertToSecureString("MyPassword")
        Dim domain As String = "MyDomain"
        Dim filename As String = "c:\windows\notepad.exe"
        Try
            System.Diagnostics.Process.Start(filename, username, password, domain)
        Catch ex As Win32Exception
            System.Windows.Forms.MessageBox.Show("Something went wrong!", "Error")
        End Try
    End Sub
    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
End Module

Open in new window

BrianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterSenior Software DeveloperCommented:
You need to use a ProcessstartInfo object like this: (Pardon my syntax here. I normally use C# and my VB is a little rusty)
Dim startInfo As ProcessStartInfo = New ProcessStartInfo("c:\windows\notepad.exe")
startInfo.UseShellExecute = false
startInfo.WorkingDirectory = "c:\windows"
startInfo.Verb = "runasuser"
startInfo.Domain = "MyDomain"
startInfo.UserName = "MyUser"
startInfo.Password = "MyPassword".ToSecureString
Dim runningProcess As Process = Process.Start(startInfo)
runningProcess.WaitForExit

Open in new window

The trick here is to use the "runasuser" verb and UseShellExecute as false.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BrianAuthor Commented:
That's awesome!  Thanks Russ!  But I am getting an error for "MyPassword".ToSecureString.  The error is 'ToSecureString' is not a member of 'String'.  Maybe I need another Imports?
Russ SuterSenior Software DeveloperCommented:
As I said, my VB is a little rusty. ToSecureString is a method. Perhaps it needs the parentheses () at the end?
Russ SuterSenior Software DeveloperCommented:
DOH! It might have been helpful if I'd have included the Extension class declaration that houses the "ToSecureString()" method.
Public Class LocalExtensions
    
    Public Shared Function ToSecureString(this ByVal password As String) As SecureString
        Dim secure As SecureString = New SecureString
        For Each c As Char In password
            secure.AppendChar(c)
        Next
        Return secure
    End Function
End Class

Open in new window

I'm not 100% certain if extension classes are allowed in VB.NET so here's another version that should work just fine.
    Public Shared Function ToSecureString(ByVal password As String) As SecureString
        Dim secure As SecureString = New SecureString
        For Each c As Char In password
            secure.AppendChar(c)
        Next
        Return secure
    End Function

Open in new window

You'd just call that as a regular function like this:
startInfo.Password = ToSecureString("MyPassword")

Open in new window

BrianAuthor Commented:
Thanks Russ for giving such clear and precise information!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.