This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Redirect Standard Output Efficiently in .NET
Capturing console output from a .NET application (C#)
I know how to execute something like this:
SomeEXE inputfile.txt
in the command prompt via C#.
The problem I am having is that SomeEXE opens another command prompt where it writes the outputs given inputfile.txt.
Is it generally possible to obtain these outputs? Thanks.
Here is my current code:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C SomeEXE inputfile.txt";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
process.StartInfo = startInfo;
process.Start();
// Now use streams to capture the output
StreamReader outputReader = process.StandardOutput;
process.WaitForExit();
String line = outputReader.ReadToEnd();
ProcessStartInfo processStartInfo = new processStartInfo("SomeEXE", "inputfile.txt");
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
// Here is where you grab the output:
processStartInfo.RedirectStandardOutput = true;
Process process = new Process {
StartInfo = processStartInfo
};
process.Start();
// Now use streams to capture the output
StreamReader outputReader = process.StandardOutput;
process.WaitForExit();
Now you can read the outputStream as necessary.
I am guessing this is what you mean. Also, here are the docs on RedirectStandardOutput
Also, if you know the path to the file that was generated (assuming the SomeEXE wrote to another file) you can use File.Open to access its contents after SomeEXE has executed (remember to wait until after otherwise SomeEXE may still have a handle on the file making it difficult to read it).
Related
This question already has answers here:
Execute multiple command lines with the same process using .NET
(10 answers)
Closed 3 years ago.
I need to run cmd commands in a c# program im writing, the only solutions ive found make a new command line instance for every command, but i need them to be executed in the same instance
Run Command Prompt Commands
public static void UpdateDeviceData()
{
Process.Start("CMD.exe",
String.Format("client_commandline.exe setdeviceposition 0 {0} {1} {2}", LPos.X, LPos.Y, LPos.Z));
}
i found a solution, its this:
Process CmdPro;
ProcessStartInfo startInfo;
startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = false;
startInfo.RedirectStandardError = false;
startInfo.FileName = "CMD.exe";
CmdPro = new Process();
CmdPro.StartInfo = startInfo; CmdPro.Start();
Console.SetOut(CmdPro.StandardInput);
I need to copy a file from one directory to another and do something with that file. I need to copy it with cmd, rather than File.Copy(), because I need the copy to be done as a part of ProcessStartInfo.
You can use this code and change startInfo.Arguments, but /C should be!
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new
System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C copy example.txt backup.txt";
process.StartInfo = startInfo;
process.Start();
You can create a bat-file to copy one or multiple files (using *). Then execute the batch file.
string batFileName = #"C:\{filePath}\copy.bat";
System.IO.File.WriteAllText(batFileName, #"copy {fileName}.{extension} {destination-filePath}");
System.Diagnostics.Process.Start(batFileName);
I was able to formulate this answer using the DOS Copy syntax along with this Stack Overflow QA
Start cmd window and run commands inside
var startInfo = new ProcessStartInfo {
FileName = "cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = new Process {StartInfo = startInfo};
process.Start();
process.StandardInput.WriteLine(#"copy c:\Source\Original.ext D:\Dest\Copy.ext");
process.StandardInput.WriteLine("exit");
process.WaitForExit();
I am working on a c# program to loop over my Windows Media Center recorded TV shows (.wtv) and convert them using the handbrake cli. I just got everything to work now and I wanted to also utilize the --scan function so that I can customize the audio and video arguments based on the input file rather then set a static.
This is what I have so far for the scan but I can't seem to find where the data is that prints out to the console window.
var p = new Process();
var pSI = new ProcessStartInfo();
pSI.RedirectStandardOutput = true;
pSI.UseShellExecute = false;
pSI.FileName = HandBrakeLocation;
pSI.Arguments = string.Concat(#"--scan -i ", '"', inputFile, '"');
pSI.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo = pSI;
p.Start();
var stdout = p.StandardOutput;//streamreader
p.WaitForExit();
I thought that perhaps the p.StandardOutput would send the console output to the stdout StreamReader variable, but I could not find it anywhere inside the object. What am i missing?
Thanks for you time and assistance.
You can read the program output using the StandardOutput property of the process (which is a stream):
var output = stdout.ReadToEnd();
p.WaitForExit();
More info can be found on MSDN: https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput%28v=vs.110%29.aspx
This question already has answers here:
Run console application from other console app
(6 answers)
Closed 9 years ago.
I make a C# console program and I want to execute some batch treatments in an other cosole.
So, I have the main program which write in the console and at a certain moment I want to execute batch treatment in an other one.
I know how execute batch treatment in the main console but I want to do that in an other one, that is my question.
How can I make that ?
EDIT :
I use a StreaWriter to write in the console like that :
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
using (StreamWriter writer = process.StandardInput)
{
if (writer.BaseStream.CanWrite)
{
// commands...
}
}
Use Process.Start:
Process.Start("cmd.exe", "yourcommandhere");
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Command prompt output being read as empty string
I have a command line program which must be executed at least once before my code can do it's groovy thang. So I use Process to run it. This appears to work.
My problem lies in the fact that I'd like to be able to see what the program says as it completes. In some situations it may return an error, which would require user intervention before proceeding. On paper, this seems trivial. Sadly, my code (below) does not seem to work:
Process p = new Process();
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = String.Format("/C adb forward tcp:{0} tcp:5574",port.ToString());
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
p.StartInfo = startInfo;
p.Start();
string adbResponse = p.StandardOutput.ReadToEnd();
p.WaitForExit();
if (adbResponse.Contains("error"))
{
// Device not connected - complain loudly
}
When I try doing this on a CMD window of my own creation, I am able to reliably induce a response containing the word error. (Specifically by unplugging something.) Under the same conditions however, the adbResponse string remains empty. What am I missing?
Console-attached processes have two different output streams. You are trapping StandardOutput but you probably want to be catching StandardError. See this question for a complete explanation (and code to safely capture both without deadlocking):
Command prompt output being read as empty string
Try something like that:
p.StartInfo.RedirectStandardOutput = true;
//p.WaitForExit();
StringBuilder value = new StringBuilder();
while ( ! p.HasExited ) {
value.Append(p.StandardOutput.ReadToEnd());
}
string result = value.ToString();