我正在尝试通过 C# 应用程序执行具有管理员权限的程序,该程序仅以用户权限调用。
代码
ProcessStartInfo psi;
try
{
psi = new ProcessStartInfo(@"WINZIP32.EXE");
psi.UseShellExecute = false;
SecureString pw = new SecureString();
pw.AppendChar('p');
pw.AppendChar('a');
pw.AppendChar('s');
pw.AppendChar('s');
pw.AppendChar('w');
pw.AppendChar('o');
pw.AppendChar('r');
pw.AppendChar('d');
psi.Password = pw;
psi.UserName = "administrator";
Process.Start(psi);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
它确实启动了 winzip,但仅限于用户权限。有没有我做错了什么,或者是否有可能启动一个具有更高权限的进程?
谢谢!
编辑: 这是问题背后的原因,也许它有助于理解我真正需要什么。
I used winzip for example to get a general idea what's incorrect with my code. The actual problem is, our company uses 2 versions of a program. But before you start any of the versions you need to import a dll file with regsvr32 (with admin rights). Now I would like to write a program that let the user select the version, import the dll and starts the correct application.
最佳答案
你需要设置ProcessStartInfo.UseShellExecute为 true
和 ProcessStartInfo.Verb到 runas
:
Process process = null;
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = "WINZIP32.EXE";
processStartInfo.Verb = "runas";
processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
processStartInfo.UseShellExecute = true;
process = Process.Start(processStartInfo);
这将使应用程序以管理员身份运行。然而,UAC 将提示用户确认。如果这是不可取的,那么您需要添加 manifest永久提升主机进程权限。
关于c# - 具有更高权限的.NET Start Process,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10121947/