Reputation: 308
I need to stop then restart Windows Update Service but I want the console to close after or not showing at all. I tried everything it says here but the console window still won't go away itself.
Process process = new Process();
process.StartInfo.FileName = "cmd";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Arguments = "/C start \"wuauserv\"";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit();
Upvotes: 0
Views: 82
Reputation: 17858
Id suggest instead of your How to stop/Start windows services
Start service
The following method tries to start a service specified by a service name. Then it waits until the service is running or a timeout occurs.
public static void StartService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch { // ... } }
Stop service
The following method tries to stop the specified service and it waits until the service is stopped or a timeout occurs.
public static void StopService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); } catch { // ... } }
You can then control the service without any console window appearing
Upvotes: 3
Reputation: 77876
Along with RedirectStandardOutput
set RedirectStandardError
to true
as well
process.startInfo.RedirectStandardOutput = true;
process.startInfo.RedirectStandardError = true;
Also make sure that UseShellExecute
must be set to false
else there is no point in setting CreateNoWindow = true
.
Upvotes: 0