I'm trying to run Rscript in Asp.net web Application using Process.start
Process.Start(#"C:\Program Files\R\R-3.4.0\bin\RScript.exe", #"F:\Project_files\R_script\RandomF_output.R");
this command produces correct output, but it shows command prompt in Browser window.
How to hide this command prompt windows from appearing.
Use ProcessStartInfo to start the process and set the CreateNoWindow property to true:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Program Files\R\R-3.4.0\bin\RScript.exe";
startInfo.Arguments = #"F:\Project_files\R_script\RandomF_output.R";
startInfo.CreateNoWindow = true;
Process.Start(startInfo);
You can also set the WindowStyle to ProcessWindowStyle.Hidden
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 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();
I have a c# console app which calls a powershell script as you can see below. If there are any errors in the powershell script, i'd love for it to report back the error to the c# console and then break in the console. How does one go about that? A code example would be greatly appreciated.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"powershell.exe";
startInfo.Arguments = argFinal;
startInfo.RedirectStandardOutput = false;
startInfo.RedirectStandardError = false;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.CreateNoWindow = false;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
Also, the powershell script itself calls some things like 7-zip and such. If they error, i would also like to capture that. Not sure if that'd work differently based on being called from powershell and not the c# console app itself.
You need to set the RedirectStandardError property to true. Then you can read the StandardError stream on the Process object to get any error text. See this MSDN topic for details.
I am using C# to interact with a program's command line interface. Everything is fine except an Error Message Window pops up when the program encounters an error. This program does not exit until the user clicks the "OK" button on the Error Window. How can I bypass this GUI window?
Note, I don't want to start this process in another desktop as indicated here, because the program will not exit until the "OK" button is clicked. I want the program to exit regardless of success or failure.
Note ProcessWindowStyle.Hidden doesn't work. My code is below.
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "path to program";
startInfo.Arguments = "command line arguments";
process.StartInfo = startInfo;
process.WaitForExit();
int exitCode = process.ExitCode;
Console.WriteLine("exit code " + exitCode);
I'm trying launch an application using c#, and have been experimentiing with the following line, if I run this from a cmd prompt it's ok but when running in my forms app it fails.
Process.Start(#"C:\Program Files (x86)\Activision\Call of Duty 4 - Modern Warfare\iw3mp.exe","+connect 91.192.210.47:2304");
The Error is Win_Improper_quit_body
any ideas.
I believe the particular executable you're trying to launch expects the working directory to be set correctly.
var processStartInfo = new ProcessStartInfo(pathToExe, args);
processStartInfo.WorkingDirectory = Path.GetDirectoryName(pathToExe);
Process.Start(processStartInfo);
See here for more info.
You should put the arguments part in Arguments property of ProcessStartInfo
Here's an example:
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);
startInfo.Arguments = "www.northwindtraders.com";
Process.Start(startInfo);