how to hide error message window produced by process started - c#

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);

Related

Hide Command Prompt in Asp.net

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

Start Process which calls a powershell script error output

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.

Telnet from command line doesn't work

I want to open Telnet session from command line via .NET.
This command works fine manually:
telnet towel.blinkenlights.nl
So i try to open it via .NET
Process process = new Process();
process.StartInfo.FileName = #"C:\windows\system32\cmd.exe";
process.StartInfo.Arguments = "telnet towel.blinkenlights.nl";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit();
I am using Wireshark to check if this start the traffic and here it seems that nothing happen and i cannot see any Telnet traffic.
If you use ProcessWindowStyle.Normal instead you would see you are not actually executing telnet. You must add the "/C" parameter if you want the CMD window to close after finishing or "/K" if you want it to remain open.
Process process = new Process();
process.StartInfo.FileName = #"C:\windows\system32\cmd.exe";
process.StartInfo.Arguments = "/k telnet towel.blinkenlights.nl";
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.Start();
process.WaitForExit();
After you get the behavior you want, then of course switch back to Hidden.

Process standard output cannot be captured?

I am launching a process and attempting to read the StdOut... I am very familiar with the normal way of launching a process, redirecting, and capturing the output.
However, I have run across an app which I can launch manually at the command prompt and see output in the console, but does not provide any output on the StdOut or StdErr streams. I have attempted launching cmd.exe first and I am able to capture the Output from cmd.exe, but not this process when launched that way either.
To be more clear, when I run app.exe manually, I see this on the console:
Trying to connect to VP
Trying to connect to VP
When I launch it from System.Diagnostics.Process directly:
[blank]
When I launch it from System.Diagnostics.Process via cmd.exe:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
Again, the class I am using to do this runs many process and has only has an issue getting the output with this app.exe, so this is not a "hey maybe try redirecting the standard output" type of problem. I know that works in my implementation.
Ideas? I've simply never encountered this before.. How can a process put something on the console window when running manually, but not when running via the Process object?
Here's my code:
process = new Process();
process.StartInfo.FileName = f.FullName;
process.StartInfo.Arguments = arguments;
process.StartInfo.WorkingDirectory = workingDir;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.OutputDataReceived += p_OutputDataReceived;
process.ErrorDataReceived += p_ErrorDataReceived;
if (!process.Start())
{
throw new Exception("Failed to start [" + f.Name + "]. Exit code " + process.ExitCode);
}
process.BeginOutputReadLine();
process.BeginErrorReadLine();
processRunning = true;
process.WaitForExit();
exitCode = process.ExitCode;

Running cmd commands with Administrator rights

How can I run the command **cd..** behind the scenes of a Windows Form? (i.e. the user cannot see it)
Thanks.
See System.Diagnostics.Process http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
There is also this SO answer to this same exact question: https://stackoverflow.com/a/1469790/25882
Example:
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 = "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();
You may initialize a new System.Diagnostics.ProcessStartInfo which has the information required for your process to start in addition to WindowStyle that indicates the window state to use when the process is started which can be Hidden, Maximized, Minimized or Normal. In your case, we will set this as Hidden so that the process which will be started won't be able to receive either input nor show the output from/to the user.
Example
System.Diagnostics.ProcessStartInfo myProcessInfo = new System.Diagnostics.ProcessStartInfo(); //Initializes a new ProcessStartInfo of name myProcessInfo
myProcessInfo.FileName = Environment.ExpandEnvironmentVariables("%SystemRoot%") + #"\System32\cmd.exe"; //Sets the FileName property of myProcessInfo to %SystemRoot%\System32\cmd.exe where %SystemRoot% is a system variable which is expanded using Environment.ExpandEnvironmentVariables
myProcessInfo.Arguments = "cd.."; //Sets the arguments to cd..
myProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Sets the WindowStyle of myProcessInfo which indicates the window state to use when the process is started to Hidden
System.Diagnostics.Process.Start(myProcessInfo); //Starts the process based on myProcessInfo
Screenshot
The following screenshot represents the Task Manager showing one process which was started by our application. However, its Window is not visible.
Notice: The process started will not terminate even if you close your application.
Additionally, to run a Process as an Administrator you may set the Verb property of the process start info to runas
Example
System.Diagnostics.ProcessStartInfo myProcessInfo = new System.Diagnostics.ProcessStartInfo(); //Initializes a new ProcessStartInfo of name myProcessInfo
myProcessInfo.FileName = Environment.ExpandEnvironmentVariables("%SystemRoot%") + #"\System32\cmd.exe"; //Sets the FileName property of myProcessInfo to %SystemRoot%\System32\cmd.exe where %SystemRoot% is a system variable which is expanded using Environment.ExpandEnvironmentVariables
myProcessInfo.Arguments = "cd.."; //Sets the arguments to cd..
myProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Sets the WindowStyle of myProcessInfo which indicates the window state to use when the process is started to Hidden
myProcessInfo.Verb = "runas"; //The process should start with elevated permissions
System.Diagnostics.Process.Start(myProcessInfo); //Starts the process based on myProcessInfo
Notice: If you have the User Account Control enabled, you may be asked to allow the process to start with elevated permissions first if the application that tried to call this process was not running with elevated permissions.
If you would like to skip the prompt, I think that you should allow your main application to start with elevated permissions. To do this, you'll need to open your application's manifest and make sure that the following line is added
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
This will simply tell your application to start only with elevated permissions. So, when you call the process as an Administrator, there'll be no prompt as the process caller is being executed under an Administrator.
Thanks,
I hope you find this helpful :)

Categories