I am trying to run batch commands in a C# application.
Usually, I would do this through the following code:
string command = "shutdown -s -t 120";
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = ("/c" + command);
process.StartInfo = startInfo;
process.Start();
However, I am building said application for a network that doesn't allow CMD.EXE. I can gain access to the Command Prompt through making a *.bat file with the "COMMAND.COM" string in it - then I have to type in the commands manually. The above code will not allow me to pass string commands to a batch file, only an *.exe file. Is there any way around this?
The answer is to bypass cmd altogether, you don't need it here, shutdown will be a process itself, so just run it directly:
Process.Start("shutdown","/s /t 120");
Shutdown isn't a batch command, it's a system executable. You can call it instead of cmd:
C:\Windows>dir /s shutdown.exe
Volume in drive C has no label.
Volume Serial Number is 008A-AC5B
Directory of C:\Windows\System32
30-10-2015 08:17 37.376 shutdown.exe
1 File(s) 37.376 bytes
Directory of C:\Windows\SysWOW64
30-10-2015 08:18 33.792 shutdown.exe
1 File(s) 33.792 bytes
So you can replace your current code with:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "shutdown.exe";
startInfo.Arguments = ("-s -t 120");
process.StartInfo = startInfo;
process.Start();
Related
I am struggling to use Chromes print to PDF feature via headless browsing.
My code is very simple
var command = "C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe --headless --disable-gpu --print-to-pdf=\"D:\\GitHub\\MySite\\bin\\Debug\\Temp\\createPdf180303084003.pdf\" http://localhost/mypage";
Process.Start(command);
When I view my command and copy the string, and paste that into a command prompt, it works fine.
This is all on one system so I don't understand why it works in command prompt and not in my C# web app.
Try this
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = #"C:\Program Files(x86)\Google\Chrome\Application\chrome.exe";
proc.Arguments = #"--headless --disable-gpu --print-to-pdf=\""D:\\GitHub\\MySite\\bin\\Debug\\Temp\\createPdf180303084003.pdf\"" http://localhost/mypage";
Process.Start(proc);
The system is now searching for the file "C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe --headless --disable-gpu --print-to-pdf=\"D:\\GitHub\\MySite\\bin\\Debug\\Temp\\createPdf180303084003.pdf\" http://localhost/mypage", but you want it to launch "C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe" with some arguments. What you need to do is:
Process process = new Process();
process.StartInfo.FileName = "chrome";
process.StartInfo.Arguments = "arguments";
process.Start();
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 performing offline operations on images taking image as a input with parameters and processing it in VTK C++ exe i am unable to pass parameters to C++ exe through C# program and retrive output .
please explain me with some example
If you just mean that you have a compiled C++ program (which we'll call "foo.exe", with path stored in string "exe_folder") and you want to call that with command line parameters (stored in string "exe_params") from C#, then the following should work:
string exe_params = "target_image.jpeg HOUGH_TRANSFORM"; // Or whatever params are appropriate.
string exe_full_path = Path.Combine(exe_folder, "foo.exe");
Process proc = System.Diagnostics.Process.Start(exe_full_path, exe_params);
https://msdn.microsoft.com/en-us/library/h6ak8zt5(v=vs.110).aspx
Let say that your executable is called test.exe and it is in the test dir. For me, the following would be working:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C cd C:\\..test\\ && test.exe target_image.jpg yourtransformation";
process.StartInfo = startInfo;
process.Start();
If you have further problems, try setting the working directory of processStartInfo.
If I run the following command in a command prompt, it works:
"C:\Program Files (x86)\AppFolder\do.exe"
If I try to run the same thing as a process:
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = #"/c ""C:\Program Files (x86)\AppFolder\do.exe""";
using (Process process = Process.Start(startInfo))
{
...
}
It does not appear to run. If I look at the startInfo.Arguments, it appears as follows:
/c \"C:\\Program Files (x86)\\AppFolder\\do.exe\"
What am I missing? Can you not pass arguments that have quotes or spaces? I see a lot of examples where people are passing items with spaces/quotes in just fine, but following them just doesn't seem to work. This is the narrowest example I can think of.
EDIT: If I just create a shortcut so I don't use spaces or quotes, it works:
startInfo.Arguments = #"/c D:\_test\Do.lnk";
I do not use '#'. This is correct implementation:
startInfo.Arguments = "/c \"C:\\Program Files (x86)\\AppFolder\\do.exe\"";
I want to compile an .asm file placed in bin folder in the masm through c#.I have tried multiple methods like process.start but nothing helps.It opens the cmd but the command "ml" never executes.It either open the pwb.exe(MASM) or the 'file.asm' in notepad.I give these arguments to CMD "path\ml file.asm" which works fine manually.ml is a command used to compile .asm files. One of the method I used is following
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = #"C:\WINDOWS\system32\cmd.exe";
startInfo.Arguments = "C:\Users\Hassan\Documents\Visual Studio 2010\Projects\FYP\FYP\MASM611\BIN\ml file.asm";
process.StartInfo = startInfo;
process.Start();
If you want to start the process in this way, you'll need to put quotes round the path, due to the spaces:
startInfo.Arguments = #"""C:\Users\Hassan\Documents\Visual Studio 2010\Projects\FYP\FYP\MASM611\BIN\ml"" file.asm";
(In a verbatim string literal, you include double-quotes by doubling them.)
Alternatively, if ml is actually an executable (I know nothing about masm) you could just use:
startInfo.FileName = #"C:\Users\Hassan\Documents\Visual Studio 2010\Projects\FYP\FYP\MASM611\BIN\ml.exe";
startInfo.Arguments = "file.asm";
To start "cmd.exe" and run another program you need to use the /c switch.
/C Carries out the command specified by string and then terminates
So you would need:
startInfo.Arguments = "/c \"C:\Users\Hassan\Documents\Visual Studio 2010\Projects\FYP\FYP\MASM611\BIN\ml.exe\" file.asm";
I've added extra quotes for the space in the path.
If you don't need to run it via the command prompt - "cmd.exe" - then you can put the path to "ml.exe" in FileName and just pass the "file.ml" as the Arguments.