Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 716
steffar Avatar asked Apr 25 '10 21:04

steffar


People also ask

How many command line arguments can be passed C?

I know for sure that there are two command-line arguments in C -- argc and argv[] , but while taking an online test, I figured out that there are 3 command line arguments in C.

Can we pass arguments in main () in C?

Yes, we can give arguments in the main() function. Command line arguments in C are specified after the name of the program in the system's command line, and these argument values are passed on to your program during program execution. The argc and argv are the two arguments that can pass to main function.

How many arguments can be passed to main () using command line arguments?

Explanation: 2 arguments are needed to be passed to main() function while using command line arguments. The first one represents a number of strings in the argument list and the second list represents the list of string arguments. 3.


1 Answers

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\"";
like image 95
jpierson Avatar answered Nov 06 '22 17:11

jpierson