I am using the following code to run a Linux console command via Mono in a C# application:
ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/bash", "-c ls");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
String result = proc.StandardOutput.ReadToEnd();
This works as expected. But, if i give the command as "-c ls -l" or "-c ls /path" I still get the output with the -l and path ignored.
What syntax should I use in using multiple switches for a command?
You forgot to quote the command.
Did you try the following on the bash prompt ?
bash -c ls -l
I strongly suggest to read the man bash.
And also the getopt manual as it's what bash use to parse its parameters.
It has exactly the same behavior as bash -c ls
Why? Because you have to tell bash that ls -l is the full argument of -c, otherwise -l is treated like an argument of bash.
Either bash -c 'ls -l' or bash -c "ls -l" will do what you expect.
You have to add quotes like this:
ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/bash", "-c 'ls -l'");
Related
i want to open the prompt command and execute arguments so i'm using this 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 = #"cd\";
process.StartInfo = startInfo;
process.Start();
But the arguments doesn't works and the prompt command open on bin/debug folder instead of executing cd\ and open on c:/
tried with :
System.Diagnostics.Process.Start("cmd.exe", #"cd\");
but didnt work either
(cd\ is just an example to see if it work the final command i need to execute is cd/ cd C:\Program Files (x86)\ffmpeg ffmpeg32 -i C:\Users\Oxitroy\Documents\instaJanvier1.mp4)
You need to add /c to the command:
System.Diagnostics.Process.Start("cmd.exe", #" /c cd\");
/c : Carries out the command specified by string and then terminates.
But try something a little more lengthy, so you can see if anything happens.
I am trying to send commands to RDP connections from a C# console application using PsExec, this is the command
PsExec.exe //1.2.3.4 -u administrator -p secredpassword -c RemoteAppExe.exe
wich works perfectly, runs RemoteAppExe.exe on that system, the problem is that within C# it dosen't work, here is my code :
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = #"PsExec.exe";
pProcess.StartInfo.Arguments = "//1.2.3.4 -u administrator -p secredpassword -c RemoteAppExe.exe"; //argument
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true; //not diplay a windows
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd(); //The output result
pProcess.WaitForExit();
What is the problem? I assume it might be the fact that arguments are not properly escaped.
Any ideeas?
Thanks.
It should be:
pProcess.StartInfo.Arguments = #"\\1.2.3.4 -u administrator -p secredpassword -c RemoteAppExe.exe";
Changing the argument line made the code work for me.
I have a php code that calls ffmpeg from the windows command line (because I run apache locally) with:
//ffmpeg -i input.mp4 -r 20 output.gif
echo exec('C:\ffmpeg\bin\ffmpeg.exe -i videos/file.avi -r 10 -s 450x230 videos/file.gif');
And it works fine! Now I wrote a C# program and I try to do the same thing with:
System.Diagnostics.Process.Start
but it fails.
Question: How do I execute the same command from the PHP code but from inside a C# code?
Process process = Process.Start(
new ProcessStartInfo
{
FileName = #"C:\ffmpeg\bin\ffmpeg.exe",
Arguments = "-i videos/file.avi -r 10 -s 450x230 videos/file.gif",
WindowStyle = ProcessWindowStyle.Hidden
}
);
process.WaitForExit();
If it fails an exception will be raised, e.g.
System.ComponentModel.Win32Exception with HResult=-2147467259 if the executable wasn't found.
I am using the following code:
Process process = new Process();
ProcessStartInfo info = new ProcessStartInfo(#"java -jar path\Ontologizer.jar -g path\go.obo -a path\gene_association.fb -m Benjamini-Hochberg -c Parent-Child-Intersection -p path\back.txt -s path\genes.txt -o path\outfull.txt");
process.StartInfo = info;
process.Start();
process.WaitForExit();
process.Dispose();
I get a Win32 exception:
The system cannot find the file specified
How can I fix this problem?
First argument of ProcessStartInfo constructor should be file name only. All arguments to application should be putted into second argument of ProcessStartInfo constructor:
new ProcessStartInfo("java", #"-jar path\Ontologizer.jar -g path\go.obo -a path\gene_association.fb -m Benjamini-Hochberg -c Parent-Child-Intersection -p path\back.txt -s path\genes.txt -o path\outfull.txt");
Is it possible to run a console application and get its outputted contents back as a string in C#?
I want to be able to use parameters when running the console app:
c:\files\app.exe -a 1 -b 2 -c 3
This isn't the clearest thing I've read today, but I can only assume you're spawning a process (with Process.Start()?) and want to get it's output back into your program.
If so, Process.StandardOutput is probably what you're looking for. For example:
System.Diagnostics.ProcessStartInfo startInfo =
new System.Diagnostics.ProcessStartInfo(#"c:\files\app.exe",#"-a 1 -b 2 -c 3");
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
Process p = Process.Start(startInfo);
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();