2011-04-19 144 views
12

我的应用使用ClickOnce tehcnology。今天我需要以管理员身份运行它。我修改清单文件从以管理员身份运行:requireAdministrator&ClickOnce +模拟系统时间

<requestedExecutionLevel level="asInvoker" uiAccess="false" /> 

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> 

然而VS不能编译项目:

错误35的ClickOnce不支持请求执行水平 'requireAdministrator'。

我认为不可能一次使用它们。不是吗?我需要更改系统时间,我可以在应用程序级别执行此操作吗?我可以模仿它,所以应用程序。可以做我想做的事。我改变时间+2小时,然后放回一秒。我有几个DLL,他们要求时间。

回答

6

时间是一个系统范围内的事情,你不能仅仅为了你的过程而改变它。对你的依赖关系说谎的唯一方法是使用Detours或类似的东西来挂钩API。如果您是低用户帐户,则不允许。

修改时间需要“更改系统时间”和/或“更改时区”权限(通常会给出管理员帐户)。

正如@Chris所述,admin和ClickOnce不兼容。

+0

那么您将如何安装需要以管理员身份运行的应用程序? – Igor 2012-05-14 21:28:25

+0

我已经成功地运行了一个ClickOnce应用程序,它需要管理员权限,方法是首先以域管理员用户身份登录,然后运行clickOnce应用程序 – JoelFan 2014-09-17 19:13:13

5

正确 - ClickOnce不具有管理员权限的操作员。事实上,它的设计不是。

21

实际上,您不能使用管理权限运行ClickOnce应用程序,但有一点小问题,您可以使用管理员权限启动新进程。 在App_Startup:

if (!IsRunAsAdministrator()) 
{ 
    var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase); 

    // The following properties run the new process as administrator 
    processInfo.UseShellExecute = true; 
    processInfo.Verb = "runas"; 

    // Start the new process 
    try 
    { 
    Process.Start(processInfo); 
    } 
    catch (Exception) 
    { 
    // The user did not allow the application to run as administrator 
    MessageBox.Show("Sorry, this application must be run as Administrator."); 
    } 

    // Shut down the current process 
    Application.Current.Shutdown(); 
} 

private bool IsRunAsAdministrator() 
{ 
    var wi = WindowsIdentity.GetCurrent(); 
    var wp = new WindowsPrincipal(wi); 

    return wp.IsInRole(WindowsBuiltInRole.Administrator); 
} 

Read full article.

但是如果你想要更多的本地和简单的解决方案只是要求用户运行Internet Explorer作为管理员,ClickOnce的工具也将具有管理员权限运行。

+0

这是一个很好的解决方法并且可行!但是你不能再读取清单文件,因为你没有使用.application,你会使用.exe本身。 – 2016-07-06 18:15:46

+0

好东西。感谢分享。 – SamekaTV 2016-07-12 14:36:54

+0

男人,你让我的这一天成真。尽管起初我犯了一个错误,那就是不检查我的应用程序是否以管理员身份运行,所以它只是在循环中反复打开相同的可执行文件。 – 2017-07-05 14:13:22

1

如果您从IE启动ClickOnce应用程序,要具有管理权限,只需使用管理权限运行IE并且您的应用程序也会拥有它。

4
private void Form1_Load(object sender, EventArgs e) 
    { 
     if (WindowsIdentity.GetCurrent().Owner == WindowsIdentity.GetCurrent().User) // Check for Admin privileges 
     { 
      try 
      { 
       this.Visible = false; 
       ProcessStartInfo info = new ProcessStartInfo(Application.ExecutablePath); // my own .exe 
       info.UseShellExecute = true; 
       info.Verb = "runas"; // invoke UAC prompt 
       Process.Start(info); 
      } 
      catch (Win32Exception ex) 
      { 
       if (ex.NativeErrorCode == 1223) //The operation was canceled by the user. 
       { 
        MessageBox.Show("Why did you not selected Yes?"); 
        Application.Exit(); 
       } 
       else 
        throw new Exception("Something went wrong :-("); 
      } 
      Application.Exit(); 
     } 
     else 
     { 
      // MessageBox.Show("I have admin privileges :-)"); 
     } 
    } 
相关问题