1、项目中添加清单文件
2、清单中修改
在项目下面会多出一个“app.manifest”的文件,选中它,并找到代码段<requestedExecutionLevel level="asInvoker" uiAccess="false" />,将其改为:<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
3、判断程序以何种权限运行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Principal;
namespace ConsoleApp4
{
class Program
{
public static bool IsAdministrator()
{
bool result;
try
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
result = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch
{
result = false;
}
return result;
}
static void Main(string[] args)
{
if (IsAdministrator())
{
Console.WriteLine("管理员");
}
else {
Console.WriteLine("非管理员");
}
Console.ReadLine();
}
}
}