博客
关于我
C#如何以管理员身份运行程序
阅读量:268 次
发布时间:2019-03-01

本文共 517 字,大约阅读时间需要 1 分钟。

在WinForm程序中获取管理员权限:Win7及以上操作系统的配置方法

在使用WinForm程序时,若在Windows 7及以上版本操作系统运行,某些操作可能需要管理员权限才能执行成功。例如,通过程序调用CMD命令或访问Windows Service服务时,若未以管理员身份运行,可能会导致命令无效或无法访问服务。

本文将介绍如何配置WinForm程序以获取管理员权限。

首先,打开Visual Studio,进入项目的属性界面。在“解决方案资源管理器”中,右键点击项目名称,选择“属性”。在弹出的窗口中,找到“安全性”选项。

接下来,启用ClickOnce安全设置。这样可以确保程序在运行时具备必要的权限。

在项目目录下会生成一个新的文件“app.manifest”。打开该文件,找到以下代码段:

将上述代码修改为:

保存修改后,重新编译项目。在运行程序时,系统会提示“用户账户控制”来获取管理员权限。点击“是”以继续运行程序。

在编译过程中,记得关闭ClickOnce安全设置勾选项,以避免程序运行时出现错误。

通过以上配置,程序将能够以管理员身份运行,从而正常执行CMD命令或访问Windows Service服务。

转载地址:http://onex.baihongyu.com/

你可能感兴趣的文章
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>