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(); 
10
  • Can you provide a program to us that builds? I'm 99% sure this one doesn't Commented Feb 23, 2016 at 15:35
  • 1
    "It doesn't like the p.StartInfo line." Exactly what is the error? Commented Feb 23, 2016 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". Commented Feb 23, 2016 at 15:37
  • Uhuh. You're using a property like a method. Commented Feb 23, 2016 at 15:38
  • 1
    Your answer is the second answer to the other question which this question is a duplicate of. You don't even need "cmd.exe" anyway. Commented Feb 23, 2016 at 15:47

2 Answers 2

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(); } 
Sign up to request clarification or add additional context in comments.

3 Comments

That was the format I was looking for. Thanks!
It gives me the error "StandardOut has not been redirected or the process hasn't started yet."
@Sean Try the edited answer.
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. 

1 Comment

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."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.