Search code examples
c#cmdcommand-line-argumentswindows-shellshellexecute

Commandline Arguments in C#


Hello again Stackoverflow community,

Today I am trying to execute an application with commandline parameters in C#, that not realy difficult like I tried

Process.Start(foldervar + "cocacola.exe", "pepsi.txt");

Cocacola.exe writes and Log in its current folder. In my commandline I write it manually like this

C:\myfolder>cocacola.exe pepsi.txt

Works wonderful but if I try it in C# a total fail.

I read that C# parses the command as C:\myfolder>cocacola pepsi.txt, without the ".EXE" ending. And I tested it manually without the ending, and this does not work.

Now, my question is what is the correct way to get C# executing it C:\myfolder>cocacola.exe pepsi.txt with the ".EXE"


Solution

  • use ProcessStartInfo

    http://www.dotnetperls.com/process-start

    example:

        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.WorkingDirectory=@"c:\someplace";
        proc.StartInfo.FileName="cocacola.exe";
        proc.StartInfo.Arguments="pepsi.txt";
        proc.Start();
        proc.WaitForExit();
    

    here is docs on the StartInfo properties:

    http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx