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");
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);
This question already has answers here:
Launching an application (.EXE) from C#?
(9 answers)
Closed 4 years ago.
For example: if one program had an alert, another separate program pop up when that alarm goes off?
Yes, you can launch another program using C# code. Here's how you do that:
static void LaunchProgram()
{
// Path to program
const string ex2 = "C:\\Dir";
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
//File name of program to launch
startInfo.FileName = "program.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
// Log error.
}
}
This question already has answers here:
Run Command Prompt Commands
(16 answers)
Closed 8 years ago.
I am new to C# code, I need to run the below commands by using C# code, Could any one give me the full code for this one?
Command :
C:\\Srinivasa\\VisualStudioProject\\CutyCapt_Pdf_Code\\release\\CutyCapt.exe
Arguments :
--url=file:///C:/Users/UPPALASX/Desktop/New%20folder/ResearchMap.html
--out=Out_Embeded_RM.png
--min-width=800
--min-height=10000
Try This:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\Srinivasa\\VisualStudioProject\\CutyCapt_Pdf_Code\\
release\\CutyCapt.exe";
startInfo.Arguments = "--url=\'C:/Users/UPPALASX/Desktop/New
folder/ResearchMap.html\' --out=Out_Embeded_RM.png --min-width=800
--min-height=10000";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
Look at Process.Start and Process.StartInfo
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).
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();