如何通过Go请求Windows上的管理权限

编程入门 行业动态 更新时间:2024-10-25 22:34:02
本文介绍了如何通过Go请求Windows上的管理权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想为我的应用程序实现的是,每当我想运行它时, 都需要右键单击并选择以管理员身份运行。我想让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上的管理权限

本文发布于:2023-11-11 06:50:05,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理权限   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!