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

Nim*_*oud 13 c# time clickonce winapi administrator

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

<requestedExecutionLevel  level="asInvoker" uiAccess="false" />
Run Code Online (Sandbox Code Playgroud)

<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
Run Code Online (Sandbox Code Playgroud)

但VS无法编译项目:

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

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

小智 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);
}
Run Code Online (Sandbox Code Playgroud)

阅读全文.

但是,如果您想要更多本机和更简单的解决方案,只需要求用户以管理员身份运行Internet Explorer,ClickOnce工具也将以管理员权限运行.


dev*_*uff 7

时间是一个系统范围的事情,你不能只为你的过程改变它.对依赖项撒谎的唯一方法是使用Detours或类似的方法挂钩API.如果您是一个低级用户帐户,则不允许.

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

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


Chr*_*ers 5

正确 - ClickOnce不能使用管理员权限操作员.事实上,它的设计并非如此.


小智 5

   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 :-)");
        }
    }
Run Code Online (Sandbox Code Playgroud)