我想要为我的应用程序实现的是,每次我想运行它时,需要右键单击并 Select Run as administrator.我希望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:访问被拒绝.

但是,如果您右键单击并以管理员身份运行,它将创建并写入该文件.

如何让它只需双击就可以请求管理员权限?

推荐答案

您需要嵌入一个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对您来说应该可以做到这一点.

Go相关问答推荐

+在具有html/模板Golang的Base64中

带有条件的for循环中缺少RETURN语句

使用ciph.AEAD.Seal()查看内存使用情况

golang testscript .txtar 语法,用于 stderr 或 stdout 中包含的文本

Go 中的sync.Cond 与 Wait 方法

Caddy服务器try 打开端口80而不是8090.

如何在 Go 服务中导入 monorepo 中的包?

带有前导零的整数参数被 flag.IntVar 解析为八进制

当填充通道的函数调用未嵌入 goroutine 时,为什么我会遇到死锁?

Go:从 ssl 证书中获取 'subject/unstructeredName' 的值

Go gmail api 快速入门导致本地主机拒绝连接 ERR_CONNECTION_REFUSED

为什么在单独的 go routine 中需要 wg.Wait() 和 close() ?

如何将具有嵌入式 struct 的 struct 展平为 json

函数超时和 goroutine 泄漏

每次有人进入我的网站时如何运行特定功能?

将 []float64 像素切片转换为图像

使用反射在 struct 内迭代切片 struct

每 N 秒运行一次函数,上下文超时

如何正确为 Go 中的值设置多种类型?

如何在gorm中处理多个查询