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)==true)
            {
                //Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                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);
            }
       这上次一位大侠给出了在Main()中获得管理员权限的代码,将代码移植到checkbox下就不行了,望指教~

解决方案 »

  1.   

    说白点就是点击checkbox后先获得权限,然后再修改注册表以实现开机自动运行(修改注册表自动运行的代码已有)~~在线等
      

  2.   

       public static void GetAdmin( )
       {
                WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
                //创建Windows用户主题
                Application.EnableVisualStyles();
                WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
                principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);            if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
                {                //创建启动对象
                    ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                    //设置运行文件
                    startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
                    //设置启动参数
                    startInfo.Arguments = String.Join(" ", Program.args);
                    //设置启动动作,确保以管理员身份运行
                    startInfo.Verb = "runas";
                    //如果不是管理员,则启动UAC
                    System.Diagnostics.Process.Start(startInfo);
                    //Application.EnableVisualStyles();
                    // Application.SetCompatibleTextRenderingDefault(false);
                    // Application.Run(new Form1());
                }
             }
      

  3.   

    别人的方法是在Main方法中调用的,检测程序启动时是否是管理员身份,如果是就直接运行,如果不是就以管理员身份"runas"来运行.你在程序中的checkbox中调用是不行的,因为它是重新以管理员身份运行的一个新程序,但是新程序里根本就没有执行到你的修改注册表的方法.建议你单独写个修改注册表的程序A.exe,放在和你现在写的程序目录一起.将GetAdmin中的方法的
    //设置运行文件
    startInfo.FileName = "A.exe路径";
    以管理员身份运行A.exe就可以了
      

  4.   

    因为通常情况下都不是管理员身份,改造main方法也可以实现.就是允许main方法接收参数.根据参数进行判断.//设置启动参数
    startInfo.Arguments = String.Join(" ", Program.args);要修改时就传一个参数进来,根剧参数来决定是否要修改注册表.修改完后直接再退出程序.这样就保证了你只有一个程序在运行了.如果不需要修改注册表就正常运行你的程序既可
      

  5.   

             我已经在Main函数设置类参数string []Args了,那个Program.args只是我在getAdmin()中对Main函数参数的引用而已具体应该怎么弄呢?~
      

  6.   


    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                if (args != null)
                {
                    if (args.Length > 0)
                    {
                        if (args[0] == "Modify")
                        {                        Modfiy();
                            Application.Exit();
                        }
                        else
                        {
                            Application.EnableVisualStyles();
                            Application.SetCompatibleTextRenderingDefault(false);
                            Application.Run(new Form1());
                        }
               
                    }
                }
            }
        }
    }Modfiy方法进行修改清册表.修改完后就退出这个程序,保证只有一个程序在运行.其它地方保持不变
      

  7.   


    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                if (args != null)
                {
                    if (args.Length > 0)
                    {
                        if (args[0] == "Modify")
                        {
                            GetAdmin();
                            Modfiy();
                            Application.Exit();
                            return;
                        }
                    }
                }            Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
    上面那个写的不够严密,看这个
      

  8.   

    还是在Main中修改程序获得管理员权限?我想要在checkbox中实现哇~~
      

  9.   

    谢谢你了,但是我的意图是想要点击checkbox后才获得管理员权限,进而修改注册表,原本在xp下是点击checkbox之后直接修改注册表的,在win7下就不行了。若是在main中获得权限,每次运行程序的时候系统都会提示是否允许该程序运行,这样多不方便啊╮(╯▽╰)╭,所以想只点击checkbox才获得管理员权限进而修改注册表~
      

  10.   

    晕倒,你没明白我的意思.
    你双击程序,程序因为没有传入参数,所以在检测 "Modify"这个参数,没有检测到就会往下继续执行.也就是运行            Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
    这样你的主界面就显示出来了.然后你点主界面的checkbox后再调用GetAdmin方法,只是这个时候要传入"Modify"这个参数,这样程序执行时就会进入
    if (args != null)
                {
                    if (args.Length > 0)
                    {
                        if (args[0] == "Modify")
                        {
                            GetAdmin();
                            Modfiy();
                            Application.Exit();
                            return;
                        }
                    }
                }
    这样就会修改注册表了,扫行完后就return,这样程序就退出了.你原来的程序还是存在的,也是有界面的
      

  11.   

    吃完饭后再试了一下...按F5运行后可以修改注册表,但是双击exe运行之后,点击checkbox就又不让修改让修改注册表了,请问是怎么一回事??
      

  12.   

    你单步调试下啊,加几个对话框看看程序走的流程是不是对的.双击exe运行之后,点击checkbox就又不让修改让修改注册表了?
    不明白你的意思,把你把的流程详细的说下把完整代码也贴上来看看
      

  13.   

      [STAThread]
            static void Main(string[]Args)
            {
                //args = Args;
                int processcount = 0;
                Process[] pro = Process.GetProcesses();
                foreach (Process isPro in pro)
                {
                    if (isPro.ProcessName == Process.GetCurrentProcess().ProcessName)
                    {
                        processcount += 1;
                    }
                }
                if (processcount > 1)
                {                //MessageBox.Show("a instance has already run!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }            else if (Args != null)
                {
                    if (Args.Length > 0)
                    {
                        if (Args[0] == "autunRun")
                        {
                            SysOper.GetAdmin(Args);
                            RegistryInfo.AutunRun(true);
                            Application.Exit();
                            return;
                        }
                    }             
                }            Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());           /* else
                {
                    Application.EnableVisualStyles();
                    Application.DoEvents();
                    Application.Run(new Form1());
                }
                */
               
            }    }
    }这是main函数的代码
      

  14.   

    就是在启动调试后,点击checkbox可以修改注册表,程序没跑出异常,但是退出调试后运行程序,点击checkbox,就弹出异常消息框了~~
      

  15.   

    晕倒,你应该把if (Args != null)
    {
    if (Args.Length > 0)
    {
    if (Args[0] == "autunRun")
    {
    SysOper.GetAdmin(Args);
    RegistryInfo.AutunRun(true);
    Application.Exit();
    return;
    }
    }防止运行多个进程的部分要放在修改注册表的后面.因为你在调试时的进程名字和你双击时的进程名字是不一样的,所以你点checkbox后可以用Process.Start的方法运行一个程序,然后就可以修改注册表了.但是你双击程序运行时,Process.Start运行的进程的名字就是一样的了,这个时候你的程序就会执行到防止程序多开的部分,而修改注册表的部分没有执行到.所以交换两者的顺序就可以了
      

  16.   

     static void Main(string[]Args)
            {
                //args = Args;
                int processcount = 0;
                Process[] pro = Process.GetProcesses();
                foreach (Process isPro in pro)
                {
                    if (isPro.ProcessName == Process.GetCurrentProcess().ProcessName)
                    {
                        processcount += 1;
                    }
                }            if (Args != null)
                {
                    if (Args.Length > 0)
                    {
                        if (Args[0] == "autoRun")
                        {
                            SysOper.GetAdmin(Args);
                            RegistryInfo.autoRun(true);
                            Application.Exit();
                            return;
                        }
                    }
                }            if (processcount > 1)
                {                //MessageBox.Show("a instance  has already run!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }                Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
     这样?ms不起作用~
      

  17.   


    static void Main(string[]Args)
    {
    //args = Args;
    if (Args != null)
    {
    if (Args.Length > 0)
    {
    if (Args[0] == "autoRun")
    {
    SysOper.GetAdmin(Args);
    RegistryInfo.autoRun(true);
    Application.Exit();
    return;
    }
    }
    }int processcount = 0;
    Process[] pro = Process.GetProcesses();
    foreach (Process isPro in pro)
    {
    if (isPro.ProcessName == Process.GetCurrentProcess().ProcessName)
    {
    processcount += 1;
    }
    }if (processcount > 1)
    {//MessageBox.Show("a instance has already run!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    Return;
    }Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
    晕倒了,说了半天你没看明白先判断要不要修改注册表,要就修改,修改完了就退出程序.
    如果不要修改,就判断当前的程序是否多开了,如果多开就退出,如果没有就继续运行出现程序的主界面.你上面的修改和之前的效果是一样的,等于没改.
      

  18.   

        public static void GetAdmin( string[]args )
            {
                WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
                Application.EnableVisualStyles();
                WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
                principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
                if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
                {
                    ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                    startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
                 
                    startInfo.Arguments = String.Join("", args);
                    startInfo.Verb = "runas";
                    System.Diagnostics.Process.Start(startInfo);
                    //退出
                   //  System.Windows.Forms.Application.Exit();
                }
             }这是GetAmin()方法,这样又问题吗?用你上面调回来后的代码也不行,无论调试运行与否,点击checkbox后都会抛出异常了~~~~
      

  19.   

         private void checkBox3_CheckedChanged(object sender, EventArgs e)
            {
                //SysOper.GetAdmin(Program.args);
                bool Flag = auntunstartup.IsRegeditExist("WinForm");
                if (checkBox3.Checked == true)
                {
                  
                    WritePrivateProfileString("IsAutunRun", "isautunrun", "Yes", FileName);
                    if (Flag == false)
                    {
                          RegistryInfo.autoRun(true);
                    }
                }
                else
                {
                    auntunstartup.DeleteRegist("WinForm");
                    WritePrivateProfileString("IsAutunRun", null, null, FileName);
                }
                
            }checkbox中的code也顺便贴出看看...
      

  20.   

    bool Flag = auntunstartup.IsRegeditExist("WinForm");
    这个是什么意思?private void checkBox3_CheckedChanged(object sender, EventArgs e)
    {
    //SysOper.GetAdmin(Program.args);
    bool Flag = auntunstartup.IsRegeditExist("WinForm");
    if (checkBox3.Checked == true)
    {WritePrivateProfileString("IsAutunRun", "isautunrun", "Yes", FileName);//这里有调用GetAdmin方法,你如果不调用程序根本就不会以管理员身分重新运行一个新的程序.SysOper.GetAdmin("autoRun");if (Flag == false)
    {
    RegistryInfo.autoRun(true);
    }
    }
    else
    {
    auntunstartup.DeleteRegist("WinForm");
    WritePrivateProfileString("IsAutunRun", null, null, FileName);
    }
    main方法中就不需要调用GetAdmin方法了.
    static void Main(string[]Args)
    {
    //args = Args;
    if (Args != null)
    {
    if (Args.Length > 0)
    {
    if (Args[0] == "autoRun")
    {
    //这里就不需要了,因为在checkbox里已经用管理员身份在运行了
    //SysOper.GetAdmin(Args);
    RegistryInfo.autoRun(true);
    Application.Exit();
    return;
    }
    }
    }int processcount = 0;
    Process[] pro = Process.GetProcesses();
    foreach (Process isPro in pro)
    {
    if (isPro.ProcessName == Process.GetCurrentProcess().ProcessName)
    {
    processcount += 1;
    }
    }if (processcount > 1)
    {//MessageBox.Show("a instance has already run!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    Return;
    }Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
      

  21.   

    bool Flag = auntunstartup.IsRegeditExist("WinForm");
    呵呵,这句是定义判断是否已经改变注册表的变量,方便下面代码对注册表的修改或者清零,auntunstarup是一个累的对象。
      

  22.   

    SysOper.GetAdmin("autoRun");这个不能这样调用吧?我的函数定义为GetAdmin(string []args),字符串不能为参数。。
      

  23.   

    没看清,那你把定义改下就好了,只需要string类型就可以的
    SysOper.GetAdmin("autoRun");
      

  24.   

       startInfo.Arguments = String.Join(" ", args);
       
    定义的参数是为了传给join的第二个参数啊。它规定了第二个参数必须为字符串数组类型的,我才应是为了接受main的参数吧
      

  25.   

       string[] str = new string[] { "autoRun"};
       SysOper.GetAdmin(str);思路对了,其它的就好说了.需要怎么改你就怎么改就可以了.
      

  26.   

      static void Main(string[] args)
            {
                if (args != null)
                {
                    if (args.Length > 0)
                    {
                        if (args[0] == "Modify")
                        {                        Modfiy();
                            Application.Exit();
                        }
                        else
                        {
                            Application.EnableVisualStyles();
                            Application.SetCompatibleTextRenderingDefault(false);
                            Application.Run(new Form1());
                        }
               
                    }
                }
    我想请问一下子if (args[0] == "Modify")
    Modify参数是修改注册表的方法吗?它又是怎么传到GetAdmin()方法中的呢 ?