0

I am trying to run a command line script from C#. I want it to run without a shell and place the output into my string output. It doesn't like the p.StartInfo line. What am I doing wrong? I am not running a file like p.StartInfo.FileName = "YOURBATCHFILE.bat" like How To: Execute command line in C#, get STD OUT results. I need to set the "CMD.exe" and command line string. I have tried p.Start("CMD.exe", strCmdText); but that gives me the error: "Memer 'System.Diagnostics.Process.Start(string,string)' cannot be accessed with an instance reference; qualify it with a type name instead."

    string ipAddress;
    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    string strCmdText;
    strCmdText = "tracert -d " + ipAdress;
    p.StartInfo("CMD.exe", strCmdText);
    string output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();
Community
  • 1
  • 1
Sean
  • 336
  • 1
  • 3
  • 15
  • Can you provide a program to us that builds? I'm 99% sure this one doesn't – Joe Phillips Feb 23 '16 at 15:35
  • 1
    "It doesn't like the p.StartInfo line." Exactly what is the error? – kjbartel Feb 23 '16 at 15:35
  • No, it doesn't and won't run, because the IP addresses are specific to my machine, and the p.StartInfo won't compile anyways. It says it "cannot be used like a method". – Sean Feb 23 '16 at 15:37
  • Uhuh. You're using a property like a method. – kjbartel Feb 23 '16 at 15:38
  • Non-invocable member 'System.Diagnostics.Process.StartInfo' cannot be used like a method. – Sean Feb 23 '16 at 15:38
  • Yes, obviously, and I am wondering how I set the CMD.exe and command line arguments. – Sean Feb 23 '16 at 15:38
  • That's pretty self explanatory. First of all, you have `ipAdress` spelled wrong. Second of all, you're using StartInfo() like a method – Joe Phillips Feb 23 '16 at 15:38
  • I saw that one, but it doesn't answer how to add "CMD.exe" and then my command. I have no file. – Sean Feb 23 '16 at 15:41
  • 1
    Your answer is the [second answer to the other question which this question is a duplicate of](http://stackoverflow.com/a/206366/1730559). You don't even need "cmd.exe" anyway. – kjbartel Feb 23 '16 at 15:47
  • I didn't see that, thanks. – Sean Feb 23 '16 at 15:49

2 Answers2

8

This code gives me the correct ouput.

const string ipAddress = "127.0.0.1";
Process process = new Process
{
    StartInfo =
    {
        UseShellExecute = false,
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        CreateNoWindow = true,
        FileName = "cmd.exe",
        Arguments = "/C tracert -d " + ipAddress
    }
};
process.Start();
process.WaitForExit();
if(process.HasExited)
{
    string output = process.StandardOutput.ReadToEnd();
}
ZwoRmi
  • 1,093
  • 11
  • 30
1

You are using StartInfo incorrectly. Have a look at documentation for ProcessStartInfo Class and Process.Start Method (). Your code should look something like this:

string ipAddress;
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
string strCmdText;
strCmdText = "/C tracert -d " + ipAdress;

// Correct way to launch a process with arguments
p.StartInfo.FileName="CMD.exe";
p.StartInfo.Arguments=strCmdText;
p.Start();


string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

Also, note that I added /C argument to strCmdText. As per cmd /? help:

/C Carries out the command specified by string and then terminates.
Serge
  • 3,986
  • 2
  • 17
  • 37
  • Yes, I tried that. It gives me the error "Member 'System.Diagnostics.Process.Start(string,string)' cannot be accessed with an instance reference; qualify it with a type name instead." – Sean Feb 23 '16 at 15:43