Take the 2-minute tour ×

I have a program that essentially all that is left is to call a command through CMD.exe .

I need to open CMD.exe in my current working directory (i know i can just do Porcess.Start(CMD.exe))

I then need to have the program TYPE a specific string into the cmd.exe. while staying in my current working directory. i cannot use "UseShellExecute == false" as it kills the shell, and i need the shell for it to work.

Apparently i need to be more specific. So, the text that i am trying to write into the cmd.exe is "clingo.exe \"Constants.txt\" \"Solver.txt\" \"Nodes.txt\" > \"Solved.txt\"". clingo.exe is an answer set compiler that i am using on the files Constants, Solver and Nodes to get a solution that is piped to Solved. all of these files are in one directory.

share|improve this question
Have you tried Process p = new Process(); p.StartInfo = new ProcessStartInfo("cmd"); p.StartInfo.Arguments = "everything else here"; –  J.Wells Nov 30 '12 at 5:23
Welcome! It looks like you're having trouble using the Process class effectively. You'll get better answers, and a better understanding of the problem, if you provide what you've tried so far. –  Cory Nelson Nov 30 '12 at 5:44
add comment

3 Answers

You can try passing argument but make sure your exe argument is correct

ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = @"C:\clingo.exe \Constants.txt\ \Solver.txt\ \Nodes.txt\ > \Solved.txt\";
proc.Arguments = @"";
share|improve this answer
what do u mean "make sure exe arg is correct"? –  Kevin Michael Strasters Nov 30 '12 at 5:37
\clingo.exe \Constants.txt\ \Solver.txt\ \Nodes.txt\ > \Solved.txt\ is actually executes when you enter it in cmd –  COLD TOLD Nov 30 '12 at 5:38
add comment

First, I'm interested in why you think UseShellExecute=false won't work for you. Is it possible you're just not using it correctly?

Here's your best option. This will work for 99% of apps:

ProcessStartInfo psi = new ProcessStartInfo
    FileName = "clingo.exe",
    Arguments = "\"Constants.txt\" \"Solver.txt\" \"Nodes.txt\"",
    RedirectStandardOutput = true,
    UseShellExecute = false

using(Process p = Process.Start(psi))
using(Stream s = File.Create("Solved.txt"))

Here's an option for the rare app which doesn't like args passed in the standard way, but works with cmd.exe:

ProcessStartInfo psi = new ProcessStartInfo
    FileName = "cmd.exe",
    RedirectStandardInput = true,
    UseShellExecute = false // note this applies to cmd.exe specifically,
                            // NOT the processes that you start from cmd.exe

using(Process p = Process.Start(psi))
    p.StandardInput.WriteLine("clingo \"Constants.txt\" \"Solver.txt\" \"Nodes.txt\" > \"Solved.txt\"");

You may need to redirect StandardOutput and StandardError as well for this one to work -- I forget!

share|improve this answer
i agree i was doing something wrong.. what you had before worked just fine.. the problem i assume was i was thinking that it would just RUN just like i had opened cmd.exe manually and typed it manually and when i hit enter manually.. i did not realize i needed to grad stantard out and do operations on it. it is now working 100% with what you had before –  Kevin Michael Strasters Nov 30 '12 at 5:54
Glad to hear it! Process.Start is a bit more low-level -- cmd.exe is calling something similar to it behind the scenes, but also working some magic of its own here. The >Solved.txt bit is not part of the process starting, but is cmd.exe-specific syntax for redirecting standard output of the command on the left of the > to the file on the right. When using Process.Start, you need to do this yourself. –  Cory Nelson Nov 30 '12 at 5:58
yea, it completely makes sense, im just 100% new to system calls from this venue (lots more practice on UNIX machines due to classes. never needed this utility until this. it appears to be working flawlessly which makes me extremely happy and relieved. and i might finally get to bed before 3am=) –  Kevin Michael Strasters Nov 30 '12 at 6:05
add comment

Why do you need to start the process from a command line? What about this?

var process = new Process { StartInfo = new ProcessStartInfo("cmd", "/c clingo.exe Constants.txt Solver.txt Nodes.txt") };
process.StartInfo.RedirectStandardOutput = true;
//do something with process.StandardOutput, this has the results

A few tricks to pay attention to here. First, use the /c argument to cmd, it will make the prompt exit right away. Second, depending on your context, you may want to set process.StartInfo.CreateNoWindow = true. Finally, note that redirecting standard output can cause the app to hang if the output exceeds the buffer. You'll want to make special effort to read that out as it comes in if you expect it to be more than a little bit of data.

share|improve this answer
The > Solved.txt bit is cmd-script for redirecting clingo's stdout into Solved.txt, so this won't work (but can be easily fixed). –  Cory Nelson Nov 30 '12 at 5:27
Also, some rare apps (ImageMagick comes to mind) will for some reason not work with this. For these you actually do need to use cmd.exe, redirecting standard input and writing commands to start the app. –  Cory Nelson Nov 30 '12 at 5:30
well as far as i can tell i have to run clingo.exe from cmd.exe for it to work propperly. but i will attempt your answer. –  Kevin Michael Strasters Nov 30 '12 at 5:30
Whoops, thanks @CoryNelson, fixed that. Also, changed to use cmd.exe –  tallseth Nov 30 '12 at 5:34
I believe that clingo.exe is one of those application.. the problem is that when i turn off the shell, it appears to not actually take any input.. –  Kevin Michael Strasters Nov 30 '12 at 5:38
add comment

Your Answer


