• C# 使用管理员权限运行程序


    最近在开发OPCServer组件过程中,在注册opcServer是总是返回false,后来查找原因得知在本地主机注册opcServer时,需要使用管理员权限。

    OPCServer在一台机器上部署时只需注册一次即可。下面代码介绍如何在 .net 程序中调用管理员权限运行方法。

    首先理清思路,将需要管理员权限执行的代码块提取出来 写成方法。

      private void btnStartRegister_Click(object sender, EventArgs e)
            {
                try
                {
                    /**
                      * 当前用户是管理员的时候,直接启动应用程序
                      * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
                      */
                    //获得当前登录的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))
                    {
                        //如果是管理员,则直接运行
                        RegisterOpc();//在这个方法里写入你自己需要调用管理员权限执行的内容
                    }
                    else
                    {
                        //创建启动对象
                        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                        startInfo.UseShellExecute = true;
                        startInfo.WorkingDirectory = Environment.CurrentDirectory;
                        startInfo.FileName = Application.ExecutablePath;
                        //设置启动动作,确保以管理员身份运行
                        startInfo.Verb = "runas";
                        try
                        {
                            System.Diagnostics.Process.Start(startInfo);
                        }
                        catch(Exception ex)
                        {
                             MessageBox.Show(ex.Message);
                        }
                        //退出
                        Application.Exit();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

    注意这里  RegisterOpc();是我封装的方法,方法里的内容就是需要管理员权限执行的操作。

            public void RegisterOpc()
            {
                string path = System.Windows.Forms.Application.ExecutablePath; //exe的路径
                bool isSuccess = OPCSrvHelper.RegisterOPCSrv(path, UUID, opcServerName);//注册OPC服务器
                if (isSuccess)
                {
                    MessageBox.Show("注册成功!");
                }
            }

    当用户第一次打开程序时,他可能并不知道需要管理员权限来执行,所以当他注册opc时,程序会弹出对话框,要求赋予管理员权限,当用户确定后。此时

    System.Diagnostics.Process.Start(startInfo);

    使用管理员权限重启启动进程,此时程序已有管理员权限,直接运行 RegisterOpc();方法。

  • 相关阅读:
    K8s PV and PVC and StorageClass
    WebSphere--WAS概念和原理解析
    K8S配置多集群访问
    ROS2学习日志:CV开发之关联Gazebo/Webots/ROS2
    CV学习日志:Orbslam使用总结
    CV学习日志:Basalt使用总结
    CV学习日志:Kalibr使用总结
    CV学习日志:CamOdoCalib使用总结
    CV学习日志:OpenVINS使用总结
    CV学习日志:搭建U2004开发环境
  • 原文地址:https://www.cnblogs.com/motion/p/15124454.html
Copyright © 2020-2023  润新知