我有一个程序需要大部分时间以普通用户身份运行,但偶尔我需要停止并启动服务。我该如何制作一个大部分时间都以普通用户身份运行但为了某些功能而提升为管理员模式的程序?
最佳答案
您无法在进程运行后提升进程,但您可以:-
以提升的方式重新启动进程
private void elevateCurrentProcess()
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = Application.ExecutablePath;
startInfo.Verb = "runas";
try
{
Process p = Process.Start(startInfo);
}
catch
{
// User didn't allow UAC
return;
}
Application.Exit();
}
此方法意味着您的流程将继续提升运行并且不再有 UAC 提示 - 好与坏取决于您的受众。
将需要提升的代码放到一个单独的exe中
将 list 设置为 requireAdministrator 并将其作为单独的进程启动。看这个sample code
此方法意味着每次运行操作时都会出现 UAC 提示。
最佳方法取决于您的受众(管理员类型与否)和提升操作的频率。
关于c# - 有时以管理员身份运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/248824/