C# 程序默认以管理员身份运行

设置程序默认以管理员身份运行

获得当前登录的Windows用户标示

System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();

判断当前登录用户是否为管理员

System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
}

完整示例代码

static void Main(string[] Args)
{
    /**
     * 当前用户是管理员的时候,直接启动应用程序
     * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
    */
    //获得当前登录的Windows用户标示
    System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
    //创建Windows用户主题
    Application.EnableVisualStyles();
    System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
    //判断当前登录用户是否为管理员
    if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
    {
        //如果是管理员,则直接运行
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
    else
    {
        //创建启动对象
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        //设置运行文件
        startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
        //设置启动参数
        startInfo.Arguments = String.Join(" ", Args);
        //设置启动动作,确保以管理员身份运行
        startInfo.Verb = "runas";
        //如果不是管理员,则启动UAC
        System.Diagnostics.Process.Start(startInfo);
        //退出
        System.Windows.Forms.Application.Exit();
    }
}

**************************************************************************************************************

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 中,你可以通过以下代码设置软件的管理员权限: ```csharp using System.Diagnostics; using System.Security.Principal; // 检查当前用户是否为管理员 bool isAdmin = new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator); if (!isAdmin) { // 创建一个启动信息对象 ProcessStartInfo startInfo = new ProcessStartInfo(); // 指定要启动的程序的路径和文件名 startInfo.FileName = Application.ExecutablePath; // 指定启动时要使用的用户名和密码 startInfo.UserName = "Administrator"; startInfo.Password = new SecureString(); string password = "123456"; // 这里需要替换为实际的管理员密码 foreach (char c in password) { startInfo.Password.AppendChar(c); } // 指定启动时使用的域名,默认为本地机器名 startInfo.Domain = Environment.MachineName; // 指定启动时的参数 startInfo.Arguments = "/restart"; // 指定启动时的工作目录 startInfo.WorkingDirectory = Application.StartupPath; // 指定启动时的窗口模式 startInfo.WindowStyle = ProcessWindowStyle.Normal; // 指定启动时的标准输入输出流 startInfo.UseShellExecute = false; startInfo.RedirectStandardInput = true; startInfo.RedirectStandardOutput = true; // 启动程序,并等待程序退出 Process process = new Process(); process.StartInfo = startInfo; process.Start(); process.WaitForExit(); } ``` 这段代码首先检查当前用户是否为管理员,如果不是,则使用 `ProcessStartInfo` 对象来启动程序,并使用管理员权限运行程序。在 `ProcessStartInfo` 对象中,我们可以指定启动时使用的用户名、密码、域名、参数、工作目录、窗口模式以及标准输入输出流等信息。最后,我们使用 `Process` 对象启动程序,并等待程序退出。 需要注意的是,使用管理员权限运行程序需要输入管理员账户的密码,这里我们将密码以明文的方式写在代码中,这并不是一个安全的做法。为了保证安全性,我们应该将密码以加密的方式保存在配置文件中,或者使用其他安全的方式来获取管理员密码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值