0

I am trying to execute a cmd command from .NET and using the find cmd command as a pipe. But the find cmd command takes the value in string but when i am creating my string variable instead of taking double quotes it is replacing it with /"".

My string variable is "/c sc QUERY ServiceName | find \"START\"" and instead of passing "/c sc QUERY ServiceName | find "START"" this to cmd shell it is passing "/c sc QUERY ServiceName | find \"START\"" and my find command is failing.

Below is my .NET code

        string cmdstr = "/c sc QUERY ServiceName | find \"START\"";
        Process prc = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = cmdstr;
        startInfo.RedirectStandardOutput = true;
        startInfo.UseShellExecute = false;
        prc.StartInfo = startInfo;

        prc.Start();

        string result;
        result = prc.StandardOutput.ReadToEnd();
        prc.WaitForExit(10);

When i debug the code the cmdstr value does not contain double quotes but it contains the \"

Nishant Shrivastava
  • 389
  • 1
  • 3
  • 17
  • 1
    Yes.. in debug, whilst hovering, it will show them. At runtime, once evaluated.. they won't be escaped. – Simon Whitehead Aug 05 '13 at 10:49
  • 1
    Have you tried @"/c sc QUERY ServiceName | find ""START""";? from here http://stackoverflow.com/questions/1594600/how-to-easily-display-double-quotes-in-strings-in-c – Damon Aug 05 '13 at 10:54
  • @Damon: That would only work in a verbatim string which the OP doesn't have. The escaping of his string is perfectly fine, the problem must be something else. – Daniel Hilgarth Aug 05 '13 at 11:47
  • @Simon: Even i thought the same and it is such a simple thing but actually it not escaping the / and not taking the double quotes. Why i am saying this because my command works fine from cmd prompt but from .NET the find command text string is having \START\ which find is not able to find. – Nishant Shrivastava Aug 05 '13 at 12:21
  • @Damon: I tried this also but during debugging the \ comes in the variable and the same gets passed to the cmd shell – Nishant Shrivastava Aug 05 '13 at 12:34
  • I'm going to take a stab in the dark at this but didn't you intent to query "State" rather than "Start"? i.e string cmdstr = "/c sc QUERY ServiceName | find \"STATE\""; Just a guess though... I've tried sc QUERY Alerter | find "STATE" and I got something a return value. – Damon Aug 05 '13 at 13:19

2 Answers2

2

Regarding your code:
It's fine and equivalent to manually open up cmd.exe and type sc QUERY ServiceName | find "START"
If i change ServiceName to W32Time and START to RUNNING, then I even get a non empty result (on my machine).

Regarding the debugger:
The debugger shows you the not evaluated version of the string (i.e. that what you actually type in the code).
If you want to see the string with evaluated escape-sequences, than you must use the "Text-Quickview"-Dialog (I don't know the exact title in English):
Open-Text-Quickview-Dialog-Button
Text-Quickview-Dialog

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
hippibruder
  • 168
  • 4
0
 string cmdstr = string.Empty;
 cmdstr = "/c sc QUERY ServiceName | find \"START\"";
 cmdstr = cmdstr.Replace("\"", "");

cmdstr ="/c sc QUERY ServiceName | find START";

-Hope it works .

Suraj Singh
  • 4,041
  • 1
  • 21
  • 36
  • What is that supposed to do? It replaces the double quotes with nothing, effectively removing them. – Daniel Hilgarth Aug 05 '13 at 11:46
  • @DanielHilgarth Have you tried it or just commented without testing it ??Is this code removing quotes???Please run it before concluding something ,and read question again the output which i provided is the desired output by questioner here . – Suraj Singh Aug 05 '13 at 11:51
  • Not correct. The line `cmdstr = cmdstr.Replace("\"", "");` removes all double quotes from `cmdstr`. Furthermore, the OP wants to pass `/c sc QUERY ServiceName | find "START"` as the parameter to `cmd.exe`. He doesn't want to pass `/c sc QUERY ServiceName | find START`. – Daniel Hilgarth Aug 05 '13 at 11:57