我想为我的应用程序实现的是,不需要右键单击并select以pipe理员身份运行,每次我想运行它。 我希望Windows提示我获得pipe理权限与其他Windows应用程序一样。
考虑下面的代码:
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) } }
如果你编译它并双击它将会打印:
打开:C:\ Windows \ test.txt:访问被拒绝。
但是,如果右键单击并以pipe理员身份运行,则会创build并写入该文件。
如何通过双击来请求pipe理权限?
您需要嵌入一个清单文件 ,告诉Windows您需要提升的权限。
该页面的示例是:
<?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>
这个疯狂的 帖子表明,使用rsrc应该为你做的伎俩。