How to run commands on Mingw from other process with C#? - c#

I am trying to execute commands on Mingw from other process with this code:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"PATH-TO-MINGW\mingwenv.cmd";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
using (Process exeProcess = Process.Start(startInfo))
{
StreamWriter str = exeProcess.StandardInput;
str.WriteLine("ls");
exeProcess.WaitForExit();
}
but this code just lunches Mingw and does not input command.
Do I miss something or it is not possible to do?
Thanks
Update
Based on Jason Huntleys answer, solution for me looks like this (I am using OMNeT++ simulator so directories are based on it)
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"PATH_TO_SIMULATOR\omnetpp-4.3\msys\bin\sh.exe";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
using (Process exeProcess = Process.Start(startInfo))
{
using (StreamWriter str = exeProcess.StandardInput)
{
str.WriteLine("cd PATH_TO_SIMULATOR/omnetpp-4.3");
str.Flush();
str.WriteLine("ls");
str.Flush();
}
exeProcess.WaitForExit();
}

I suspect c# is launching your mingw command in a CMD prompt. You need to spawn your process within a bash shell. Try wrapping your command with "bash -l -c 'ls'" or "bash -c 'ls'". Make sure bash is in your PATH, and be sure you quote command arguments, if any. I've had to use this method when I spawn bash commands from popen in python. I know diff language, but could be related.
I imagine the code will look similar to this. I haven't tested in C#:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "bash.exe";
startInfo.Arguments = "-l -c 'ls -l /your/msys/path'";
# Or other examples with windows path:
# startInfo.Arguments = "-l -c 'ls -l /c/your/path'";
# startInfo.Arguments = "-l -c 'ls -l C:/your/path'";
# startInfo.Arguments = "-l -c 'ls -l C:\\your\\path'";
process.StartInfo = startInfo;
process.Start();

you should do
str.Flush();
so the command you've writen is passed to the process.
also you should use using statement when dealing with streams
using (Process exeProcess = Process.Start(startInfo))
{
using(StreamWriter str = exeProcess.StandardInput)
{
str.WriteLine("ls");
str.Flush();
exeProcess.WaitForExit();
}
}

Related

Access is denied error when using ProcessStartInfo

I'm trying to run SoX command using ProcessStart but it's giving me an error with Access denied, i've made sure all admin access are in place not sure what else could be causing this error?
var startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Data\sox1441";
startInfo.Verb = "runas";
startInfo.Arguments = ("sox -c 2 -b 24 -r 48000 --buffer 8000 -t waveaudio -d apple.wav trim 0 3");
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = false;
startInfo.WorkingDirectory = #"C:\Data\";
using (Process soxProc = Process.Start(startInfo))
{
soxProc.WaitForExit();
}

Merge audio and video in c#? [duplicate]

I'm currently trying to combine two files, one audio, and one video, in my C# program using FFMPEG with the following code:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.FileName = "ffmpeg.exe";
startInfo.Arguments = "ffmpeg -i video.mp4 -i mic.wav -c:v copy -map 0:v:0 -map 1:a:0 -c:a aac -b:a 192k output.mp4";
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
My debug folder is set up like this:
So I don't get any runtime errors, however, it just doesn't create the final output.mp4 file that I need it to.
I ended up solving it by doing the following:
string args = "/c ffmpeg -i \"video.mp4\" -i \"mic.wav\" -shortest outPutFile.mp4";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = #"" + outputPath;
startInfo.Arguments = args;
using Process exeProcess = Process.Start(startInfo)
exeProcess.WaitForExit();
For some reason answer given by TermSpar didn't work for me at all, code that worked for me:
ProcessStartInfo startInfo = new()
{
CreateNoWindow = false,
FileName = #"C:\Users\User\Documents\ffmpeg.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
Arguments = string.Format(" -i {0} -i {1} -shortest {2} -y", #"C:\images\video.avi", #"C:\images\voice.wav", #"C:\images\result.avi")
};
using Process exeProcess = Process.Start(startInfo);
//debug
string StdOutVideo = exeProcess.StandardOutput.ReadToEnd();
string StdErrVideo = exeProcess.StandardError.ReadToEnd();
//
exeProcess.WaitForExit();
exeProcess.Close();

Combine audio and video files with FFMPEG in C#

I'm currently trying to combine two files, one audio, and one video, in my C# program using FFMPEG with the following code:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.FileName = "ffmpeg.exe";
startInfo.Arguments = "ffmpeg -i video.mp4 -i mic.wav -c:v copy -map 0:v:0 -map 1:a:0 -c:a aac -b:a 192k output.mp4";
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
My debug folder is set up like this:
So I don't get any runtime errors, however, it just doesn't create the final output.mp4 file that I need it to.
I ended up solving it by doing the following:
string args = "/c ffmpeg -i \"video.mp4\" -i \"mic.wav\" -shortest outPutFile.mp4";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = #"" + outputPath;
startInfo.Arguments = args;
using Process exeProcess = Process.Start(startInfo)
exeProcess.WaitForExit();
For some reason answer given by TermSpar didn't work for me at all, code that worked for me:
ProcessStartInfo startInfo = new()
{
CreateNoWindow = false,
FileName = #"C:\Users\User\Documents\ffmpeg.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
Arguments = string.Format(" -i {0} -i {1} -shortest {2} -y", #"C:\images\video.avi", #"C:\images\voice.wav", #"C:\images\result.avi")
};
using Process exeProcess = Process.Start(startInfo);
//debug
string StdOutVideo = exeProcess.StandardOutput.ReadToEnd();
string StdErrVideo = exeProcess.StandardError.ReadToEnd();
//
exeProcess.WaitForExit();
exeProcess.Close();

How to restart IIS on remote windows server from windows 7 client machine?

I am trying to restart iis remotely (Windows Servr 2012) from my local machine (Windows 7). The below command in command line doesn't work to restart IIS;
iisreset servername /restart
but the below command works fine when I tried in command line.
psexec iisreset \\servername /restart
Now the issue is when I try with below code in C#,
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "\C psexec iisreset \\servername /restart";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
// capture what is generated in command prompt
var output = process.StandardOutput.ReadToEnd();
If I give any other arguments in the above code like 'ipconfig', it gives me the expected output. But when I try with psexec, it gives empty output. But it works well when tried in command prompt.
I have also tried by using 'psexec.exe' in the filename and by removing '\C psexec' in the arguments. But still no luck.
Could you please anyone help me to resolve this?
Thanks in Advance.
I have found that when using PSexec like this that you Shouldn't use CMD.exe, and you need to ensure that you have the full path to psexec. Even if it is in the same directory as your application exe.
//Assume that psexec.exe is in same location as application
//Get directory of running applications
String AppPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\","");
//Set up start infor details
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false;
//Combine path of running app
startInfo.FileName = Path.Combine(AppPath, "psexec.exe");
startInfo.Arguments = "\\servername c:\path\to\iisreset /restart";
//Execute
Process nProc = Process.Start(startInfo);
nProc.Start();
I hope u need to provide the domain admin credentials to it.
private int ExcecuteCommand(string command, string fileName, bool getResult, string timeout = null)
{
try
{
var secure = new SecureString();
foreach (char c in txtAdminPassword.Text)
{
secure.AppendChar(c);
}
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.Domain = txtDomainName.Text;
pProcess.StartInfo.UserName = txtUser.Text;
pProcess.StartInfo.Password = secure;
pProcess.StartInfo.FileName = fileName;// AppDomain.CurrentDomain.BaseDirectory + #"PSTools\PsExec.exe"; ;
//pProcess.StartInfo.Arguments = string.Format(#"\\{0} -i -s -accepteula ipconfig /all", ipAddress);
//pProcess.StartInfo.Arguments = string.Format(#"\\{0} -accepteula netstat -ano",ipAddress);
//pProcess.StartInfo.Arguments = string.Format(#"\\{0} -accepteula -i CheckURLConnectivity", ipAddress);
//pProcess.StartInfo.Arguments = string.Format(#"\\{0} -accepteula ping {2}", ipAddress, AppDomain.CurrentDomain.BaseDirectory + #"Installer\CheckURLConnectivity.exe","10.10.10.35");
//pProcess.StartInfo.Arguments = string.Format(#"\\{0} -accepteula cmd /c type C:\ServiceLog.txt", ipAddress);
pProcess.StartInfo.Arguments = command;//string.Format(#"\\{0} -accepteula -c -f {1}", compName, AppDomain.CurrentDomain.BaseDirectory + #"Installer\CheckURLConnectivity.exe");
pProcess.StartInfo.UseShellExecute = false;
Process.StartInfo.RedirectStandardInput = true;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.RedirectStandardError = true;
pProcess.StartInfo.CreateNoWindow = true;
logger.log("Query " + command);
pProcess.Start();
if (timeout == null)
pProcess.WaitForExit();
else
pProcess.WaitForExit(Convert.ToInt32(timeout));
string strOutput = string.Empty;
if (pProcess.HasExited == true && pProcess.ExitCode != 0)
{
string _errorMessage = GetWin32ErrorMessage(pProcess.ExitCode);
pProcess.Kill();
return pProcess.ExitCode;
}
else
return 0;
}
catch (Exception)
{
return -1;
}
}
IISreset requires elevated privilege to work.So you have to use the -h switch with psexec
-h If the target system is Vista or higher, has the process run with the account's elevated token, if available.
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "psexec.exe";
startInfo.Arguments = "-h iisreset \\servername /restart";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
// capture what is generated in command prompt
var output = process.StandardOutput.ReadToEnd();
Thank you all for your valuable response. It works fine with below code :)
startInfo.FileName = #"C:\Windows\Sysnative\PsExec.exe";
startInfo.Arguments = "iisreset \\servername /restart";
Reference: Process.Start in C# The system cannot find the file specified error

Trouble running Git from C#

I'm trying to run some Git commands from a C# program using System.Diagnostics.Process and it's not working. Git is in my path and when I try to run the command at the command prompt it works fine. I've tried capturing the standard output using a nested using() statement and that's not helping. When it gets to the reader.ReadToEnd() it just shows the DOS window hung with nothing in it. Here's my code:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
startInfo.WorkingDirectory = "c:\\gitmover";
startInfo.FileName = "cmd.exe";
startInfo.Arguments = " git add \"*.*\"";
using (Process process = Process.Start(startInfo))
{
using (StreamReader reader = process3.StandardOutput)
{
string result = reader.ReadToEnd();
}
}
Any ideas what I could be doing wrong?

Categories