C#管理者として実行するプログラムの作成

1573 ワード

using System;



using System.Collections.Generic;



using System.Linq;



using System.Windows.Forms;







namespace MyWebBrowser



{



    static class Program



    {



        /// <summary>



        ///  。



        /// </summary>



        [STAThread]



        static void Main()



        {



            // 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))



            {



                // ,  



                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.Verb = "runas";



                // , UAC 



                System.Diagnostics.Process.Start(startInfo);



                //  



                System.Windows.Forms.Application.Exit();



            }



        }



    }



}