WPF开发之以管理员身份运行
时间:2022-07-22
本文章向大家介绍WPF开发之以管理员身份运行,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
管理员运行
1.打开项目的属性
2.选择“安全性”,勾选启用ClickOnce安全设置
3.然后会在Properties
里自动生成 app.manifest
打开app.manifest
中修改为如下配置
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
</requestedPrivileges>
可用设置
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
4.如果现在直接运行会报错
所以要在之前的属性里取消勾选启用ClickOnce安全设置
。
现在运行程序就会要求以管理员身份运行了。
注意
尽管程序的默认用户账户控制是
asInvoker
,在以管理员身份运行的vs里对其他程序的调用也会以管理员身份(以当前调用权限运行)。
代码形式
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();
}
}
}
}
判断程序是否以管理员运行
using System.Security.Principal;
public bool IsAdministrator()
{
WindowsIdentity current = WindowsIdentity.GetCurrent();
WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current);
return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
注册表写入
#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
- 【前端】ACE Editor(代码编辑器) 简易使用示例
- LOJ#6277. 数列分块入门 1
- 【Ruby on Rails】Model中关于保存之前的原值和修改状态
- 一个很逗的东西——Jd
- SQL Server 数据库清除日志的方法
- 算法模板——平衡树Treap 2
- DTS,DTC, DTB你都认识吗?
- LOJ#6278. 数列分块入门 2
- 1901: Zju2112 Dynamic Rankings
- LOJ#6279. 数列分块入门 3
- 1984: 月下“毛景树”
- 3212: Pku3468 A Simple Problem with Integers
- 1036: [ZJOI2008]树的统计Count
- 2243: [SDOI2011]染色
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法