I need to execute two commands on the cmd. Despite my research I have not found a workable solution to my problem. First I need to cd to directory and then run an exe in that directory.
using (Process process = new Process())
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.WorkingDirectory = #"C:\Program Files\Blacksmith\bin\apache\bin";
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = #" \c httpd.exe";
// Redirects the standard input so that commands can be sent to the shell.
process.StartInfo.RedirectStandardInput = true;
process.OutputDataReceived += ProcessOutputDataHandler;
process.ErrorDataReceived += ProcessErrorDataHandler;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
I'm trying to execute httpd.exe through cmd.exe to stop apache from being run as a windows service.
Would this work for you?
using (Process process = new Process())
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = #"C:\Program Files\Blacksmith\bin\apache\bin\httpd.exe";
// Redirects the standard input so that commands can be sent to the shell.
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.OutputDataReceived += ProcessOutputDataHandler;
process.ErrorDataReceived += ProcessErrorDataHandler;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
Try this
using (Process process = new Process())
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.WorkingDirectory = #"C:\Program Files\Blacksmith\bin\apache\bin";
process.StartInfo.FileName = "httpd.exe";
// Redirects the standard input so that commands can be sent to the shell.
process.StartInfo.RedirectStandardInput = true;
process.OutputDataReceived += ProcessOutputDataHandler;
process.ErrorDataReceived += ProcessErrorDataHandler;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
I think that you might try /c instead of \c
Related
my intention is to start a Powershell process with admin rights to pass it arguments in C#. For that I have written following piece of code
public void PassCommand(string command)
{
Process process = new Process();
process.StartInfo.FileName = "powershell.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Verb = "runas";
process.Start();
process.StandardInput.WriteLine(command);
process.StandardInput.Flush();
process.StandardInput.Close();
Console.WriteLine(process.StandardOutput.ReadToEnd());
}
I have read in some other articles thatI just have to add this line process.StartInfo.Verb = "runas";. But unfortunately it does not work. The Powershell starts without admin rights. Can someone help me to get this solved?
I have multiple commands and I wanted to execute them in a single line using Process. The first command is using a For loop.
for /f ... do xxxx...
then after finishing the command above execute another multiple commands.
& [command] & [command] & [command] & [command]
I understand the use of & and && operator so I tried the following command and no result.
for /f ... do xxxx... & [command] & [command] & [command] & [command]
To summarize the issue, the problem occurred after finishing the for loop, the second command is not executed which should be executed with or without error because of & operator.
My current solution is to use a separated process method and call after finishing the for loop command.
private string void cmd_command(){
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/K";
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WorkingDirectory = workingDirectory;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine(#"for /f ... do xxxx...");
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
string response = process.StandardOutput.ReadToEnd();
Console.WriteLine(response);
if(response.ToUpperInvariant().Contains(expectedOutput)){
//call second multiple command
return next_cmd_command();
}
return null;
}
private string next_cmd_command(){
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/K";
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WorkingDirectory = workingDirectory;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine(#"[command] & [command] & [command] & [command]");
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
string response = process.StandardOutput.ReadToEnd();
Console.WriteLine(response);
return response;
}
I used this code to execute multi cmd commands in C#.
First i created constructor to create process.
public CMD()
{
process = new Process();
startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "cmd.exe";
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();
process.EnableRaisingEvents = true;
process.StandardInput.AutoFlush = true;
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.OutputDataReceived += Process_OutputDataReceived;
process.ErrorDataReceived += Process_ErrorDataReceived;
process.Exited += Process_Exited;
}
then i used this code to execute multi commands.
public void _cmd(string command)
{
using (StreamWriter sw = process.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine(command);
}
}
}
for the first command is not any problem but when i send second one i got this error in sw.BaseStream.CanWrite line.
Object reference not set to an instance of an object.
when i debug code for the first time sw.BaseStream is OK but in second command it get null
what is problem here ?
I suspect it's because of your using statement. When the using block is exited, it will call Dispose in process.StandardInput().
I am writing a shell in C# using a Console App. Using my Shell I would execute other applications such as ping.exe, takeown.exe etc just like you would with cmd or bash.
I have no problem in redirecting Standard Out from the other process to my Console App. The issue is that I don't know how to redirect StardardIn properly so that I can interact with the running application. I.e say I need to enter "y" to confirm an action using cacls.exe.
Here is my code for StandardOut:
How can I write back to the App?
Process process = new Process();
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = cmd;
process.StartInfo.Arguments = Arguments;
process.OutputDataReceived += new DataReceivedEventHandler(
(s, e) =>
{
Console.WriteLine(e.Data);
}
);
process.ErrorDataReceived += new DataReceivedEventHandler((s, e) => { Console.WriteLine(e.Data); });
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
You should write back the data to the app by using process.StandardInput.WriteLine(message)
and message should be a string.
You can use Write() or WriteLine()
I am running a WebDeploy package deployment command ([myapp].deploy.cmd) using the following code:
Process process = new Process();
process.StartInfo.FileName = FileName;
process.StartInfo.Arguments = Args;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.EnableRaisingEvents = true;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.ErrorDataReceived += DataReceived;
process.OutputDataReceived += DataReceived;
process.WaitForExit();
return process.ExitCode;
it always returns an exit code of 0 even when the command fails. I have running the command directly from the command line with the same arguments and it does exit with code 1 (echo %ERRORLEVEL%) so I there must be something in the way I am starting the process that means this is not caught by the Process instance?