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;
}
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 am trying to start a process on Mac and Windows (using Unity) to run FFMPEG to convert a video to a .ogv video. My code is as follows:
string command = "ffmpeg -i '" + filepath + "' -codec:v libtheora -qscale:v 10 -codec:a libvorbis -qscale:a 10 -y '"+workingDir+"/ogv_Video/"+System.IO.Path.GetFileNameWithoutExtension(filepath)+".ogv'";
UnityEngine.Debug.Log("Command: "+command);
try{
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo (workingDir+"/..", command);
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.FileName =workingDir+"/ffmpeg";
//Process.Start (startInfo);
Process p = Process.Start(startInfo);
p.EnableRaisingEvents = true;
string strOutput = p.StandardOutput.ReadToEnd();
UnityEngine.Debug.Log ("Running..."+strOutput);
p.WaitForExit();
UnityEngine.Debug.Log ("Got here. "+strOutput);
int exitCode = p.ExitCode;
UnityEngine.Debug.Log ("Process exit code = "+exitCode);
}
catch(Exception e) {
UnityEngine.Debug.Log ("An error occurred");
UnityEngine.Debug.Log ("Error: "+e);
}
The command executes and does not through any exception. However, it terminates instantly and prints Exit Code 1 which is "Catchall for general errors" -this seems not too helpful!
What am I doing wrong with my code, please?
You'll notice that my code prints out the command in full. If I copy that command and paste it into the terminal, it runs absolutely fine.
It turns out I was setting up the arguments wrongly. Referring to this Stack Overflow question, I was able to produce the expected result with the following code:
try{
Process process = new Process();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.FileName = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) +#"ffmpeg";
process.StartInfo.Arguments = command;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
JSONDataObject rtnMsg = new JSONDataObject("StartConvertOK", "-1", new List<string>());
return JsonUtility.ToJson(rtnMsg);
}
It does seem as though the answer was not that different from what I was doing, but it does work!
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?
I have an app.exe application that asks to enter input path string, once i enter, it asks output path string... now when i enter, app.exe perform some operation
i need to pass these paths from my Window Form Application
i saw a lot of questions like this but could not implement what i require because i never worked with processes and Stream Reader or Writer
any help please... examples will be thanked.. thank you..
string input = #"C:\Documents and Settings\pankaj\Desktop\My File\greetingsfreinds.ppt";
string output = #"C:\Documents and Settings\pankaj\Desktop\test";
Process process = new Process();
process.StartInfo.FileName = #"C:\Program Files\Wondershare\MyApp\app.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.RedirectStandardInput = true;
process.Start();
process.WaitForExit(3000);
process.Close();
ok i tried that
but its giving some exception
StandardOut has not been redirected or the process hasn't started yet...
my code was
string input = #"C:\Documents and Settings\pankaj\Desktop\My File\greetingsfreinds.ppt";
string output = #"C:\Documents and Settings\pankaj\Desktop\test";
Process process = new Process();
process.StartInfo.FileName = #"C:\Program Files\Wondershare\MyApp\app.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.Arguments = input + ";" + output;
process.Start();
string Strout = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
You can use ProcessStartInfo.Arguments for this.
Process process = new Process()
process.StartInfo.FileName = #"C:\Program Files\Wondershare\MyApp\app.exe";
process.StartInfo.UseShellExecute = false;
....
process.Arguments = input + " " + output;
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