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%") + @"System32cmd.exe"; //Sets the FileName property of myProcessInfo to %SystemRoot%System32cmd.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%") + @"System32cmd.exe"; //Sets the FileName property of myProcessInfo to %SystemRoot%System32cmd.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 :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…