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

本文关键字:amp ClickOnce+ 模拟系统 时间 requireAdministrator 管理员 身份 运行 | 更新日期: 2023-09-27 17:51:22

我的应用程序使用ClickOnce技术。今天我需要以管理员身份运行它。我修改了的清单文件

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

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

但是VS无法编译项目:

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

我认为不可能同时使用它们。不是吗?我需要更改系统时间,我可以在应用程序级别更改吗?我可以模仿它吗。可以做我想做的事。我把时间改成+2小时,然后放回一秒钟。我收到了一些dll,他们问我时间。

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

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

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);
}

阅读全文。

但如果您想要更原生、更简单的解决方案,只需要求用户以管理员身份运行Internet Explorer,ClickOnce工具也将以管理员权限运行。

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

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

正如@Chris所提到的,admin和ClickOnce不兼容。

正确-ClickOnce不能使用管理员权限进行操作。事实上,它的设计并不是这样的。

   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 :-)");
        }
    }

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

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