2009-10-27 94 views
6

我有这个应用程序需要做一些保护路径的事情(如%PROGRAMFILES%),我知道我应该使用%APPDATA%,但我无法改变目前。我已经分离出所有可能需要UAC来显示在另一个项目的事情,这里有一个示例代码:皮条客我的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消息,与节目标志和所有这些事情,让我告诉你一个例子:

Ugly UAC

Personalized UAC

哪有我为我的程序做了那个?

3)为了避免在使用提升特权运行时进入循环,它会得到另一个UnauthorizedAccessException我做了那件事传递任何参数。你会做什么来实现相同的目标?

我认为这就是现在。谢谢你的时间。

+0

关于颜色,那些取决于程序。快速查看http://news.softpedia.com/images/news2/Windows-Vista-UAC-Colors-3.png。 – sisve 2009-10-27 17:05:45

回答

4

1您无法控制托管DLL的进程的提升模式。如果你可以控制安装过程,你可以为大家grant permission to the target folder or registryduring the install process。您需要登录sign the program with a certificate published by a certificate authority that would be trusted by the client。访问您的本地证书存储(控制面板 - > Internet选项,内容选项卡,发布者)以查看常见证书颁发机构。

3当您获取UnauthorizedAccessExceotion时,将其引入宿主exe文件或返回错误值,指示安全问题。您的DLL的调用者然后决定要执行什么操作,例如显示安全错误对话框以通知用户程序是否已经提升(权限未由域控制器授予?),或者如果未提升,则为restarting the process in elevated mode using the runas command

6

我有同样的问题。搜索大约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; 
    } 
+0

你是一个拯救生命的人。谢谢,这个工作很好。 – GR7 2013-06-28 21:54:49