我正在尝试从用 C#(在 .net 4.0 上)编写的帮助应用程序中以编程方式重新启动服务,但是如果我在右键单击并执行“以管理员身份运行”时双击运行 EXE 会出现权限冲突。
但是为什么我需要这个用户是本地管理员?!
我希望该应用程序能够正常运行,并且仅在用户单击按钮以重新启动服务时才请求管理员权限。这可以做到吗?
该解决方案需要在 xp、vista 和 windows 7 上运行。
我正在使用来自http://www.csharp-examples.net/restart-windows-service/的代码
public static void RestartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
int millisec1 = Environment.TickCount;
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
// count the rest of the timeout
int millisec2 = Environment.TickCount;
timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1));
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch
{
// ...
}
}