如何通过Go请求Windows上的管理权限 [英] How to ask for administer privileges on Windows with Go

查看:794
本文介绍了如何通过Go请求Windows上的管理权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的应用程序实现的是,每当我想运行它时, 都需要右键单击并选择以管理员身份运行。我想让Windows提示我像其他Windows应用程序一样获得管理权限。



考虑以下代码:

<$ (
fmt
io / ioutil
time


func main(){
err:= ioutil.WriteFile(C:/Windows/test.txt,[] byte(TESTING!),0644)
如果err!= nil {
fmt.Println(err.Error())
time.Sleep(time.Second * 3)
}
}

如果您编译它并双击它,它会打印:

< blockquote>

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上的管理权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆