简体   繁体   中英

Passing a command to custom command prompt from C#

I am trying to run a command in a custom .cmd from C#. The problem is that the command is not passed to the prompt when the command prompt is ready. Here is my code:

proc1.UseShellExecute = false;
string Command = @"Depanalyzer targets /n Dev\Tools\CleanupPerfCounters.exe";
proc1.FileName = @"C:\Users\xx\Desktop\MyCustom.cmd";
proc1.RedirectStandardOutput = true;
proc1.Verb = "runas";
proc1.Arguments ="/k " +  Command;
p.Start();
string output = p.StandardOutput.ReadToEnd();

I see command window opened but the Command not passed to it and there is no output.

Your Command is passed as an argument and that argument looks like it as whitespace. To pass an argument with whitespace you need to wrap that argument in quotes.

proc1.UseShellExecute = false;
//wrap Command in quotes
string Command = @"""Depanalyzer targets /n Dev\Tools\CleanupPerfCounters.exe""";
proc1.FileName = @"C:\Users\xx\Desktop\MyCustom.cmd";
proc1.RedirectStandardOutput = true;
proc1.Verb = "runas";
//add quotes
proc1.Arguments ="/k " +  Command;
p.Start();
string output = p.StandardOutput.ReadToEnd();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM