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

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

 

  

但VS无法编译项目:

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

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

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

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

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

实际上,您无法使用管理权限运行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); } 

阅读全文。

但是,如果您想要更多本机和更简单的解决方案,只需要求用户以管理员身份运行Internet Explorer,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,您的应用程序也将拥有它。