前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF开发之以管理员身份运行

WPF开发之以管理员身份运行

作者头像
码客说
发布2020-08-02 17:57:09
2K0
发布2020-08-02 17:57:09
举报
文章被收录于专栏:码客码客

管理员运行

1.打开项目的属性

2.选择“安全性”,勾选启用ClickOnce安全设置

3.然后会在Properties里自动生成 app.manifest

img
img

打开app.manifest中修改为如下配置

代码语言:javascript
复制
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
    <requestedExecutionLevel level="highestAvailable" uiAccess="false" />
</requestedPrivileges>

可用设置

代码语言:javascript
复制
<requestedExecutionLevel  level="asInvoker" uiAccess="false" />
<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

4.如果现在直接运行会报错

所以要在之前的属性里取消勾选启用ClickOnce安全设置

现在运行程序就会要求以管理员身份运行了。

注意

尽管程序的默认用户账户控制是asInvoker,在以管理员身份运行的vs里对其他程序的调用也会以管理员身份(以当前调用权限运行)。

代码形式

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using System.Security.Principal;
using System.Threading;
using System.Windows;
using SchoolClient.Utils;
using SchoolClient.Wins;

namespace SchoolClient
{
    public partial class MyApp : Application
    {
        [STAThread]
        private static void Main()

        {
            new MyApp();
        }

        public MyApp()
        {
            /**
            * 当前用户是管理员的时候,直接启动应用程序
            * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
            */
            //获得当前登录的Windows用户标示
            WindowsIdentity identity = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(identity);
            //判断当前登录用户是否为管理员
            if (principal.IsInRole(WindowsBuiltInRole.Administrator))
            {
                //如果是管理员,则直接运行
                Run(new LoginWindow());
            }
            else
            {
                //创建启动对象
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                startInfo.UseShellExecute = true;
                startInfo.WorkingDirectory = Environment.CurrentDirectory;
                startInfo.FileName = Assembly.GetExecutingAssembly().Location;
                //设置启动动作,确保以管理员身份运行
                startInfo.Verb = "runas";
                try
                {
                    System.Diagnostics.Process.Start(startInfo);
                }
                catch
                {
                    return;
                }
                //退出
                Application.Current.Shutdown();
            }
        }
    }
}

判断程序是否以管理员运行

代码语言:javascript
复制
using System.Security.Principal;
public bool IsAdministrator()
{
    WindowsIdentity current = WindowsIdentity.GetCurrent();
    WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current);
    return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}

注册表写入

代码语言:javascript
复制
#region 开机自启动写入注册表

private void RunAtStart()
{
    var starupPath = GetType().Assembly.Location;
    try
    {
        var fileName = starupPath;
        var shortFileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
        //打开子键节点
        var myReg = Registry.LocalMachine.OpenSubKey(
            "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", RegistryKeyPermissionCheck.ReadWriteSubTree,
            RegistryRights.FullControl);
        if (myReg == null)
        {
            //如果子键节点不存在,则创建之
            myReg = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
        }
        if (myReg != null && myReg.GetValue(shortFileName) != null)
        {
            //在注册表中设置自启动程序
            myReg.DeleteValue(shortFileName);
            myReg.SetValue(shortFileName, fileName);
            LogHelper.WriteLog(typeof(MainWindow), "设置自启动程序操作成功");
        }
        else if (myReg != null && myReg.GetValue(shortFileName) == null)
        {
            myReg.SetValue(shortFileName, fileName);
            LogHelper.WriteLog(typeof(MainWindow), "设置自启动程序操作成功");
        }
    }
    catch
    {
        LogHelper.WriteLog(typeof(MainWindow),"写注册表操作发生错误");
    }
}

#endregion
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 管理员运行
  • 代码形式
  • 判断程序是否以管理员运行
  • 注册表写入
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档