zoukankan      html  css  js  c++  java
  • How do I force my .NET application to run as administrator?

    How do I force my .NET application to run as administrator?

    You'll want to modify the manifest that gets embedded in the program. This works on Visual Studio 2008 and higher: Project + Add New Item, select "Application Manifest File". Change the <requestedExecutionLevel> element to:

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

    The user gets the UAC prompt when they start the program. Use wisely; their patience can wear out quickly.

    How do I make a console app always run as an administrator?

    First, understand that there WILL BE a UAC dialog at some point in this process. There is no way to avoid it. There are a few approaches you can take:

    • Have the people run the ERP software elevated. I am including this only for completeness. It is awful to have to consent to the UAC prompt every time you run the app when you usually don't need the powers. It would be a handy quick test, though, for you to confirm that if your app is elevated, things will work. Everything launched from an elevated process is elevated, so if your app still gets the error message, this isn't something you'll fix by elevating.
    • Change the code in the ERP app to launch your app elevated. You mention C#. Launching with the runas verb is an approach here. This puts the burden on the ERP developer and they may not be able to do it.
    • Add a manifest to your app. You can embed one, or if your app is called foo.exe just hand-create a foo.exe.manifest file with the appropriate requestedExecutionLevel. To embed one, use the Properties page of your C# project to choose the right kind of manifest. Make sure you're launched with UseShellExecute set to true, or the manifest will be ignored.

    If you choose the first option, the UAC prompt will be every time the ERP app launches. Not good. If you choose the second or third, the UAC prompt will be every time your console app is launched from the ERP app. Probably acceptable.

    One other thing you might consider is looking far more seriously into why the console app needs admin privs. Are you writing to the root of C or to Program Files? Are you updating a registry key in HKLM? Whatever you're doing, why are you doing it that way? Unless your app installs or configures something (in which case getting a UAC prompt is good and proper) you should try to adapt it so that it writes to pre-user storage and doesn't need elevation. Then you will no longer be worrying about how to launch it elevated, in any case.

  • 相关阅读:
    多线程自动化运维linux的尝试 优化二
    多线程自动化运维linux的尝试 优化一
    多线程自动化运维linux的尝试
    mysql root 没有任何权限
    oracle hint 知多少
    pandas 实战笔记
    ICM issue IcmPlAllocBuf: MpiGetOutbuf failed (rc = 14 (MPI_ESTALE: outdated MPI handle))
    利用python在Oracle数据库中生成密码字典库
    nginx 配置的一些参数
    nginx四层代理
  • 原文地址:https://www.cnblogs.com/chucklu/p/10855068.html
Copyright © 2011-2022 走看看