views:

541

answers:

3

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: http://stackoverflow.com/questions/667381/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

A: 

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.

Raj Kaimal
A: 

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

voltagex
+1  A: 

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\"";
jpierson
I tried this one, I also tried adding cmd parameters such as /k (continue execution), but still nothing works.
teff