4

I am coding a program in C# and I need to open cmd.exe, send my commands and get its answers. I searched around and found some answers to take diagnostics.process in use.

Now, I have two problems:

  1. When I get the output of process, the output is not shown on the cmd consoule itself.
  2. I need to call g95 compiler on the system. When I call it from cmd manually, it is invoked and does well, but when I call it programmatically, I have the this error: "g95 is not recognized as an internal or external ..."

On the other hand, I only found how to send my commands to cmd.exe via arguments and process.standardInput.writeline(). Is there any more convenient method to use. I need to send commands when the cmd.exe is open.

I am sending a part of my code which may help:

System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");

//myProcess.StartInfo.Arguments = "/c g95";
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(myProcess_OutputDataReceived);
myProcess.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(myProcess_ErrorDataReceived);

myProcess.Start();
myProcess.BeginOutputReadLine();
myProcess.BeginErrorReadLine();
myProcess.StandardInput.WriteLine("g95 c:\\1_2.f -o c:\\1_2.exe");
4
  • 1
    Why do you need cmd.exe? Why don't you do the above directly with g95.exe ? Commented Jun 10, 2014 at 9:56
  • 1
    What about: codeproject.com/Articles/335909/… Commented Jun 10, 2014 at 10:05
  • Thank you @Kris. But I don't understand you Commented Jun 10, 2014 at 11:42
  • 2
    Kriss meant, that you can start g95 process directly with specific arguments. There is no need in CMD here at all Commented Jun 11, 2014 at 9:24

2 Answers 2

3

You can specify the g95 directly and pass the desired command line parameters to it. You don't need to execute cmd first. The command may not be regognized because the settings from the user profile are not loaded. Try setting the property LoadUserProfile in StartInfo to true.

myProcess.StartInfo.LoadUserProfile = true;

This should also set the path variables correctly. Your code would look something like this:

Process myProcess = new Process();
myProcess.StartInfo = new ProcessStartInfo("g95");

myProcess.StartInfo.Arguments = "c:\\1_2.f -o c:\\1_2.exe"
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.LoadUserProfile = true;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.OutputDataReceived += myProcess_OutputDataReceived;
myProcess.ErrorDataReceived += myProcess_ErrorDataReceived;

myProcess.Start();
myProcess.BeginOutputReadLine();
myProcess.BeginErrorReadLine();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. It's work fine. I use it for open cmd through bat file and send to it lots of cmds =) process.StartInfo.CreateNoWindow = true; process.StandardInput.WriteLine(cmd1); process.StandardInput.WriteLine(cmd2); process.StandardInput.WriteLine("exit"); process.WaitForExit(); process.Close();
0

You are getting the error

"g95 is not recognized as an internal or external ..."

because you haven't added the path to g95.exe in your PATH environment variable. You will get similar result if you open up command prompt and type g95. Here is a link to G95 Windows FAQ page that explains it.

1 Comment

Thank you for your reply. But if I open the cmd up and type the command manually, I don't get this error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.