steffar
steffar

Reputation: 389

How can I pass more than one command line argument via c#

I need to pass more than one command line argument via c# for a process called handle.exe: http://www.google.com.mt/search?sourceid=chrome&ie=UTF-8&q=handle.exe

First, I need to run the executable file via ADMINISTRATOR permissions. This post has helped me achieve just that: programatically run cmd.exe as adminstrator in vista, c#

But then comes the next problem of calling the actual line arguments such as "-p explore"

How can I specify the command line arguments together, or maybe consecutively?

Current code is as follows:

        Process p = new Process();
        ProcessStartInfo processStartInfo = new ProcessStartInfo("filePath");
        processStartInfo.CreateNoWindow = true;
        processStartInfo.UseShellExecute = false;
        processStartInfo.RedirectStandardOutput = true;
        processStartInfo.RedirectStandardInput = true;
        processStartInfo.Verb = "runas";
        processStartInfo.Arguments = "/env /user:" + "Administrator" + " cmd";

        p.StartInfo = processStartInfo;
        p.Start();
        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();
        Console.WriteLine(output);      

Thanks

Upvotes: 7

Views: 3122

Answers (3)

jpierson
jpierson

Reputation: 17356

I believe the answer you are looking for is right out of the Runas command documentation.

runas /user:[email protected] "notepad my_file.txt" 

It appears that the last argument to the runas command is the command that is being run along with any arguments. The key is to use quotes to group the actual command executable with it's arguments so that the values are not seen as separate arguments to the runas command but instead is issued as a single command on it's own.

So in your example you might want to do the following.

processStartInfo.Arguments = "/env /user:" + "Administrator" + " \"cmd -p explore\"";

Upvotes: 1

Adam
Adam

Reputation: 2087

If you are trying to run a process with elevated permissions, there may be a better way than calling runas.

Upvotes: 0

Raj Kaimal
Raj Kaimal

Reputation: 8304

You can run the process using the UseShellExecute command and pass in the username and password

http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.useshellexecute%28v=VS.100%29.aspx

http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.username%28v=VS.100%29.aspx

Although you will be storing the username and password somewhere.

Upvotes: 0

Related Questions