uacまたはuserlevelそれを克服する方法!
-
28-09-2019 - |
質問
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
{
// ...
}
}
解決
このような新しいコンソールアプリプロジェクトを作成します。
public class RunService
{
static int Main()(string[] args)
{
//TODO read serviceName and timeoutMilliseconds from args
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);
// TODO return status code
}
catch
{
// ...
}
}
}
プロジェクトでは、アッパープロジェクトへの参照を追加し、このようなものを使用して実行可能ファイルを呼び出します。 Runas動詞の使用ではなく、ユーザーに昇格の権利を促します。
var process = Process.Start(new ProcessStartInfo
{
Verb = "runas",
FileName = typeof(RunService).Assembly.Location,
UseShellExecute = true,
CreateNoWindow = true,
});
process.WaitForExit();
var exitCode = process.ExitCode
// TODO process exit code...
他のヒント
UAC許可リクエストをスキップ/無視することはできません(それは本質的にそれが全体の使用であることを否定するでしょう)が PrincipalPermissionAttribute あなたが探しているものかもしれません。しかし、これまでに使用したことはありません。
所属していません StackOverflow