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
{
// ...
}
}