我正在为应用程序创建一个自动更新程序。该应用程序由用户启动,无需管理员权限即可运行。自动更新程序以管理员权限启动,并在下载新文件之前终止应用程序。
当我想在 autoupdater 完成后启动更新的应用程序时,问题就来了。如果我使用常规的 System.Diagnostics.Process.Start(file),应用程序也会以管理员权限启动,并且它必须在当前用户上运行才能按预期工作。
那么,如何让自动更新程序以当前用户而不是管理员身份启动应用程序呢?
我试过使用以下方法:
var pSI = new ProcessStartInfo() {
UseShellExecute = false,
UserName = Environment.UserName,
FileName = file
};
System.Diagnostics.Process.Start(pSI);
但这会引发错误“无效的用户名或密码”。我已经检查过用户名是否正确,并且我知道密码可能无效,因为我没有包含它。但是要求用户输入他/她的密码不是一个选项,因为自动启动应用程序的全部原因是为了让用户更容易。
有什么建议吗?
最佳答案
您想要实现的目标无法轻易完成,并且不受支持。但是,可以使用少量黑客攻击。 Aaron Margosis 写了一篇 article描述一种技术。
要引用相关部分,您需要执行以下步骤:
- Enable the SeIncreaseQuotaPrivilege in your current token
- Get an HWND representing the desktop shell (GetShellWindow)
- Get the Process ID (PID) of the process associated with that window (GetWindowThreadProcessId)
- Open that process (OpenProcess)
- Get the access token from that process (OpenProcessToken)
- Make a primary token with that token (DuplicateTokenEx)
- Start the new process with that primary token (CreateProcessWithTokenW)
本文包含一些演示 C++ 源代码的下载链接,从中应该足够简单,可以将其转换为 C#。
关于c# - 如何从具有管理员权限的进程启动没有管理员权限的新进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11169431/