• WIN8,开机启动 需要管理员权限的程序


    1. 用WPF开发,需要管理员权限并开机启动,以写注册表的方式实现。仅写注册表只能启动一般权限的程序。

    2. 考虑这样实现: 程序以一般权限启动,启动以后申请管理员权限。

    实现:

    App类中重写OnStartup事件,判断当前程序的权限,如果不是管理员权限,则申请权限,重启进程并关闭当前进程:

                    WindowsIdentity identity = WindowsIdentity.GetCurrent();
                    WindowsPrincipal principal = new WindowsPrincipal(identity);
                    if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
                    {
                        if (IsRunningOne(fileName))
                        {
                            MessageBox.Show("already exit");
                            Application.Current.Shutdown();
                            Environment.Exit(0);
                        }
                        ProcessStartInfo startInfo = new ProcessStartInfo();
                        startInfo.FileName = Assembly.GetExecutingAssembly().Location;
                        startInfo.Verb = "runas";
    
                        Process.Start(startInfo);
                        Current.Shutdown();
                        Environment.Exit(0);
                    }    
    

      如果已经有实例运行,则不启动。

     3.  开机启动的程序,获取当前exe所在路径的值为“C:WindowsSystem32”

     App类:,其中不允许运行两个实例。

    public partial class App : Application
        {
            string fileName = "SystemManager";
    
            private bool IsRunning(string fileName)
            {
                return Process.GetProcessesByName(fileName).Length > 2;
            }
    
            private bool IsRunningOne(string fileName)
            {
                return Process.GetProcessesByName(fileName).Length > 1;
            }
    
            protected override void OnStartup(StartupEventArgs e)
            {
                if (Debugger.IsAttached)
                {
                    base.OnStartup(e);
                    return;
                }
                if (!IsRunning(fileName))
                {
    
                    WindowsIdentity identity = WindowsIdentity.GetCurrent();
                    WindowsPrincipal principal = new WindowsPrincipal(identity);
                    if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
                    {
                        if (IsRunningOne(fileName))
                        {
                            MessageBox.Show("already exit");
                            Application.Current.Shutdown();
                            Environment.Exit(0);
                        }
                        ProcessStartInfo startInfo = new ProcessStartInfo();
                        startInfo.FileName = Assembly.GetExecutingAssembly().Location;
                        startInfo.Verb = "runas";
    
                        Process.Start(startInfo);
                        Current.Shutdown();
                        Environment.Exit(0);
                    }
                }
                else
                {
                    MessageBox.Show("实例运行超过2个");
                }
                //base.OnStartup(e);
            }
    
        }
  • 相关阅读:
    Http接口安全设计
    RTMP服务器搭建(nginx+rtmp)
    OSI七层协议详解
    TCP协议的3次握手与4次挥手过程【深度详解】
    new和delete的深层次剖析(C++)
    大小端模式详解
    MP4文件格式分析及分割实现(附源码)
    使用HBuilder将H5的项目打包成手机可安装的webapp程序(.apk)
    Centos7 解决odoo10打印条形码显示方框乱码的问题
    linux 中运行Django项目
  • 原文地址:https://www.cnblogs.com/pangkang/p/6230347.html
Copyright © 2020-2023  润新知