从非管理员应用程序以管理员身份运行流程
从未以管理员身份运行的应用程序,我有以下代码:
ProcessStartInfo proc = new ProcessStartInfo(); proc.WindowStyle = ProcessWindowStyle.Normal; proc.FileName = myExePath; proc.CreateNoWindow = false; proc.UseShellExecute = false; proc.Verb = "runas";
当我调用Process.Start(proc)时,我没有弹出请求以管理员身份运行的权限,并且exe不以管理员身份运行。
我尝试将app.manifest添加到myExePath中找到的可执行文件,并将requestedExecutionLevel更新为
使用更新的app.manifest,在Process.Start(proc)调用中,我得到一个exception,“请求的操作需要提升。”
为什么.Verb操作没有设置管理员权限?
我正在测试Windows Server 2008 R2 Standard。
您必须使用ShellExecute
。 ShellExecute是唯一知道如何启动Consent.exe
以提升的API。
示例(.NET)源代码
在C#中,调用ShellExecute
的方法是使用Process.Start
和UseShellExecute = true
:
private void button1_Click(object sender, EventArgs e) { ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\Notepad.exe"); info.UseShellExecute = true; info.Verb = "runas"; Process.Start(info); }
如果你想成为一名优秀的开发人员,你可以在用户点击No时捕获:
private void button1_Click(object sender, EventArgs e) { const int ERROR_CANCELLED = 1223; //The operation was canceled by the user. ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\Notepad.exe"); info.UseShellExecute = true; info.Verb = "runas"; try { Process.Start(info); } catch (Win32Exception ex) { if (ex.NativeErrorCode == ERROR_CANCELLED) MessageBox.Show("Why you no select Yes?"); else throw; } }
奖金观看
- UAC – 什么。 怎么样。 为什么。 。 UAC的体系结构,解释说
CreateProcess
不能进行提升,只创建一个进程。ShellExecute
是知道如何启动Consent.exe的人,而Consent.exe是检查组策略选项的人。
注意 :任何代码都会发布到公共领域。 无需归属。
- 如何使用Entity Framework以DataGridView可编辑和上下文跟踪更改的方式过滤数据?
- 如何使用Lucene.Net执行“包含”搜索而不是“以’开头”
- .NET 4中的URL重写?
- Azure Web应用程序缓慢的数据库连接
- MVC 4 ViewModel没有被发送回Controller
- 带有Entity Framework的SQLite DATETIME列
- IIS FTP 7.5可扩展性(IFtpLogProvider并将FTP故障记录到事件日志中)
- 避免从multithreadingc#MVVM应用程序中的ViewModel对象调用BeginInvoke()
- 使用WebClient.DownloadFileAsync时如何处理exception