我想为我的应用程序实现的是,每当我想运行它时, 都需要右键单击并选择以管理员身份运行。我想让Windows提示我像其他Windows应用程序一样获得管理权限。
考虑以下代码: <$ (fmtio / ioutiltime) func main(){ err:= ioutil.WriteFile(C:/Windows/test.txt,[] byte(TESTING!),0644)如果err!= nil { fmt.Println(err.Error()) time.Sleep(time.Second * 3)} }
如果您编译它并双击它,它会打印:
open:C:\Windows\test.txt:访问被拒绝。
但是如果右键单击并以管理员身份运行,它将创建并写入该文件。
如何通过双击它来请求管理权限? / p>
解决方案您需要嵌入清单文件,它会告诉Windows你需要提升权限。
该页面的示例是:
<?xml version =1.0encoding =UTF-8standalone =yes?> < assembly xmlns =urn:schemas-microsoft-com:asm.v1manifestVersion =1.0> < assemblyIdentity version =9.0.0.0 processorArchitecture =x86 name =myapp.exe type =win32 /> < description>我的应用程序< / description> < trustInfo xmlns =urn:schemas-microsoft-com:asm.v3> < security> < requestedPrivileges> < requestedExecutionLevel level =requireAdministratoruiAccess =false/> < / requestedPrivileges> < / security> < / trustInfo> < / assembly>这 go-nuts post 建议使用 rsrc 应该为你解决问题。 / p>
What I want to achieve for my application, is to not need to right click and choose Run as administrator each time I want to run it. I want Windows to prompt me to gain admin permissions as with other windows applications.
consider the following code:
package main import ( "fmt" "io/ioutil" "time" ) func main() { err := ioutil.WriteFile("C:/Windows/test.txt", []byte("TESTING!"), 0644) if err != nil { fmt.Println(err.Error()) time.Sleep(time.Second * 3) } }If you compile it and double click on it it will print:
open: C:\Windows\test.txt: Access is denied.
But if you right-click and run as administrator, it will create and write the file.
How to make it ask for the admin permission just by double clicking on it?
解决方案You need to embed a manifest file which will tell Windows that you want elevated privileges.
The example from that page is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity version="9.0.0.0" processorArchitecture="x86" name="myapp.exe" type="win32" /> <description>My App</description> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> <security> <requestedPrivileges> <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/> </requestedPrivileges> </security> </trustInfo> </assembly>This go-nuts post suggests that using rsrc should do the trick for you.
更多推荐
如何通过Go请求Windows上的管理权限
发布评论