gpt4 book ai didi

windows - 如何使用 Go 在 Windows 上请求管理员权限

转载 作者:IT老高 更新时间:2023-10-28 13:05:50 30 4
gpt4 key购买 nike

我想为我的应用程序实现的是不需要每次我想运行它时都需要右键单击并选择以管理员身份运行。我希望 Windows 像其他 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)
}
}

如果你编译它并双击它会打印出来:

open: C:\Windows\test.txt: Access is denied.

但如果你右键单击并以管理员身份运行,它会创建并写入文件。

如何让它通过双击请求管理员权限?

最佳答案

您需要嵌入 manifest file这将告诉 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>

这个 go-nuts post建议使用 rsrc应该为您解决问题。

关于windows - 如何使用 Go 在 Windows 上请求管理员权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31558066/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com