running shell command
my application will run a command that is like this:
wco -f "C:\Work\6.70 Ex\Master Build.Txt"
what i do is i usually open up cmd and type the above line manually.
i tried to automate this with a script:
string strCmdText = "wco -f C:\Work\6.70 ex\Maste开发者_开发知识库r Build.Txt";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
however because of the white spaces it gets confusing. please help
If you want to use cmd.exe to run a program, you need to add either the /C
or /K
switch. The /C switch runs the command and then exits cmd.exe. /K runs the command and then leaves cmd.exe open.
cmd.exe /K echo hello
I assume wco
is a program of yours? If so, you can bypass using cmd.exe and just call wco.exe directly.
You need to escape your arguments just like you do on the command line plus you need to escape your backslashes:
string strCmdText = "wco -f \"C:\\Work\\6.70 ex\\Master Build.Txt\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
You will need to use the escape character, which is \
in C#.
string strCmdText = "wco -f \"C:\Work\6.70 ex\Master Build.Txt\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
However, if "wco" is an executable, the actual code you should use is
string strCmdText = "-f \"C:\Work\6.70 ex\Master Build.Txt\"";
System.Diagnostics.Process.Start("wco", strCmdText);
This will probably make it easier to redirect the output.
精彩评论