How to request administrator permissions when the program starts?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 18:33:20
manojlds

Add the following to your manifest file:

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

You can also use highestAvailable for the level.

Look here about embedding manifest files:

http://msdn.microsoft.com/en-us/library/bb756929.aspx

PS: If you don't have a manifest file, you can easily add a new one:

In Visual Studio, right click project -> Add Item -> Choose Application Manifest File ( under General for Visual C# items)

The added file will already have the above part, just change the level to requireAdministrator from asInvoker

Put this XML in a file called yourexename.exe.manifest:

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
   <security>
     <requestedPrivileges>
        <requestedExecutionLevel level="highestAvailable" />
     </requestedPrivileges>
   </security>
</trustInfo>
</assembly>
George

For F# Visual Studio 2013, including a manifest file that request administrator elevation using the FSharp compiler's /win32manifest flag as follows worked for me. So, given a project output named "App.Exe"

  1. Create a file with the following content (for convenience you may add the file to the project. Ensure that it's Build Action is None' andCopy to Output...isDo not copy. By convention such a file is named App.Exe.manifest`. If you require uiAccess (User Interface), the assembly must be strongly named.

    <?xml version="1.0" encoding="utf-8" ?>
    <asmv1:assembly manifestVersion="1.0" 
        xmlns="urn:schemas-microsoft-com:asm.v1"
        xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
        xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <assemblyIdentity version="1.0.0.0" name="App" />
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
          <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
          </requestedPrivileges>
        </security>
      </trustInfo>
    </asmv1:assembly>
    
  2. Edit the project dialogue build panel's Other flags: entry field to include the following: /win32manifest:<ApplicationManifestFile>. For example, in this case, /win32manifest:App.Exe.manifest.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!