皮条客我的UAC和一些关于它的问题

我有这个应用程序需要在受保护的路径中做一些事情(如%PROGRAMFILES%),我知道我应该使用%APPDATA%,但我现在无法改变它。 我已经隔离了所有可能需要UAC出现在另一个项目上的东西,这里是一个示例代码:

using System; using System.Diagnostics; using System.IO; using System.Windows.Forms; class Class1 { static void Main(string[] args) { try { File.CreateText(Path.Combine(Application.StartupPath, "something.txt")); } catch (UnauthorizedAccessException ex) { MessageBox.Show(ex.Message, "UnauthorizedAccessException", MessageBoxButtons.OK, MessageBoxIcon.Error); if (args.Length == 0) { Process proc = new Process(); proc.StartInfo.FileName = Application.ExecutablePath; proc.StartInfo.Arguments = "not again"; proc.StartInfo.Verb = "runas"; proc.Start(); } else { MessageBox.Show("Exit to avoid loop."); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } 

因此,我从我的主程序中调用此可执行文件,如果由于未经授权的访问而失败,它将自动启动显示UAC请求。

我的问题是:

1)我不得不将项目输出从DLL转换为EXE,因为我找不到任何方法从DLL请求UAC提升,有没有简单的方法呢?

2)我还注意到有些程序显示了个性化的UAC消息,带有程序徽标和所有这些东西,让我给你看一个例子:

丑陋的UAC

个性化的UAC

我怎么能为我的程序做到这一点?

3)为了避免在使用提升的权限运行时进入循环,它会获得另一个UnauthorizedAccessException我做了那个传递任何args的东西。 你会做些什么来实现同样的目标?

我认为这一切都是现在。 谢谢你的时间。

1您无法控制托管DLL的进程的提升模式。 如果可以控制安装过程,则可以在安装过程中为每个人授予目标文件夹或注册表的权限 。

2您需要使用由客户端信任的证书颁发机构发布的证书对程序进行签名 。 访问您当地的证书商店(控制面板 – >互联网选项,内容选项卡,发布者)以查看通用证书颁发机构。

3当您获得UnauthorizedAccessExceotion时,将其抛给托管exe或返回指示安全问题的错误值。 然后,DLL的调用者决定要做什么,例如显示安全错误对话框以通知用户程序是否已被提升(域控制器未授予权限?),或者使用runas命令以提升模式重新启动进程它没有boost。

我有同样的问题。 谷歌搜索大约2天我找到了唯一符合我需求的解决方案 – 以管理权限启动应用程序。 我启动应用程序,检查它是否以管理员身份运行。 如果没有 – 用管理权限重新启动它。

  static void Main(string[] args) { if (NeedElevation(args) && Elevate(args)) { // If elevastion succeeded then quit. return; } // your code here } public static bool Elevate(string[] args) { try { ProcessStartInfo info = Process.GetCurrentProcess().StartInfo; info.Verb = "runas"; info.Arguments = NoElevateArgument; foreach (string arg in args) { info.Arguments += ' ' + arg; } info.FileName = Assembly.GetEntryAssembly().Location; Process process = new System.Diagnostics.Process(); process.StartInfo = info; process.Start(); } catch (Exception) { MessageBox.Show("You don't have administrative privileges thus the Automatic Application Updates cannot be started. But the rest of application is available as usually.", "Not enough user rights", MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } return true; }