以管理员身份运行:requireAdministrator& ClickOnce +模拟系统时间

时间:2011-04-19 08:49:31

标签: c# winapi time clickonce administrator

我的应用使用ClickOnce tehcnology。今天我需要以管理员身份运行它。我从

修改了清单文件
<requestedExecutionLevel  level="asInvoker" uiAccess="false" />

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

但VS无法编译项目:

  

错误35 ClickOnce不支持请求执行级别'requireAdministrator'。

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

6 个答案:

答案 0 :(得分:25)

实际上您无法使用管理员权限运行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工具也将以管理员权限运行。

答案 1 :(得分:7)

时间是系统范围内的事情,您无法仅为您的流程进行更改。对依赖项撒谎的唯一方法是使用Detours或类似的方法挂钩API。如果您是一个低级用户帐户,则不允许。

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

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

答案 2 :(得分:5)

正确 - ClickOnce不能具有管理员权限。事实上,它的设计并非如此。

答案 3 :(得分: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 :-)");
        }
    }

答案 4 :(得分:2)

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

答案 5 :(得分:0)

我的应用访问注册表,因此需要管理员权限,因此我的项目属性/安全性选项卡设置为完整权限。我将clickonce应用程序部署到网络驱动器。我只能以管理员身份运行ClickOnce“ setup.exe”来安装我的应用程序。但是,安装后我无法运行我的应用程序。我无法以管理员身份运行我的应用程序,但是我可以返回setup.exe并再次以管理员身份运行,这将运行我的应用程序,因为它已经安装。通过Win 10兼容性向导将我的应用设置为始终以提升的权限运行不起作用。我不喜欢告诉我的用户,他们必须始终右键单击并以管理员身份从网络驱动器运行setup.exe。最后,我不会“部署” clickonce应用程序。我只是构建到网络驱动器,而我的用户运行我的应用程序的exe。然后,他们可以使用兼容性向导始终以admin身份执行。

相关问题