以管理员身份运行:requireAdministrator &单击一次 +模拟系统时间

发布于 2024-11-02 08:48:30 字数 492 浏览 7 评论 0原文

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

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

但是

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

VS 无法编译该项目:

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

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

My app uses ClickOnce tehcnology. Today I needed to run it as administrator. I modified the manifest file from

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

to

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

However VS cannot compile the project:

Error 35 ClickOnce does not support the request execution level 'requireAdministrator'.

I think it's impossible to use them at once. Isn't it? I need to change the system time, can I do that in application level? Can I emulate it, so app. can do what I want. I change time +2 hours then put back for a second. I got a few dlls and they ask for time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你怎么这么可爱啊 2024-11-09 08:48:30

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

Actually You can't run ClickOnce application with Administrative privileges but there is a little hack, you can start new process with Administrator privileges.
In 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.

But if you want more native and easier solution just ask a user to run Internet Explorer as administrator, ClickOnce tool also will run with admin rights.

暖树树初阳… 2024-11-09 08:48:30

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

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

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

Time is a system-wide thing, you can't change it just for your process. The only way to lie about it to your dependencies is to hook the API, using Detours or something similar. Not allowed if you're a lowly user account.

Modifying the time requires the "Change the system time" and/or "Change the time zone" privileges (which the Administrator account is normally given).

And as mentioned by @Chris, admin and ClickOnce aren't compatible.

耀眼的星火 2024-11-09 08:48:30

正确 - ClickOnce 无法使用管理员权限进行操作。事实上,它的设计并非如此。

Correct - ClickOnce cannot operator with Administrator priviledges. In fact, it is designed not to.

依 靠 2024-11-09 08:48:30
   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 :-)");
        }
    }
   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 :-)");
        }
    }
神经大条 2024-11-09 08:48:30

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

If you launching ClickOnce app from IE, to have Administrative privileges just run IE with Administrative privileges and your app will have it too.

深海少女心 2024-11-09 08:48:30

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

My app accesses the registry and as such, requires administrator permissions so my project properties / security tab is set for full rights. I deploy my clickonce app to a network drive. I can only run the ClickOnce "setup.exe" as administrator to install my app. However, I'm unable to run my app after it's installed. I cannot run my app as administrator, but I can go back to the setup.exe and run as administrator again which will run my app since it is already installed. Going through the Win 10 Compatibility wizard to set my app to always run with elevated permissions doesn't work. I don't like telling my users they have to always right click and run the setup.exe as administrator from the network drive. In the end, I don't "deploy" the clickonce app. I just build to a network drive and my users run my app's exe. They're then able to use the compatibility wizard to execute as admin all the time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文