첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ Program.cs

using System;
using System.Diagnostics;
using System.Security.Principal;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    static class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        [STAThread]
        private static void Main()
        {
            if(IsAdministratorRole())
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
            else
            {
                MessageBox.Show("관리자 권한으로 프로그램을 다시 실행합니다.");

                try
                {
                    ProcessStartInfo processStartInfo = new ProcessStartInfo();

                    processStartInfo.UseShellExecute  = true;
                    processStartInfo.FileName         = "TestProject.exe";
                    processStartInfo.WorkingDirectory = Environment.CurrentDirectory;
                    processStartInfo.Verb             = "runas";

                    Process.Start(processStartInfo);
                }
                catch(Exception exception)
                {
                    Console.WriteLine(exception.ToString());
                }

                return;
            }
        }

        #endregion
        #region 관리자 역할 여부 구하기 - IsAdministratorRole()

        /// <summary>
        /// 관리자 역할 여부 구하기
        /// </summary>
        /// <returns>관리자 역할 여부</returns>
        private static bool IsAdministratorRole()
        {
            WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
 
            if(windowsIdentity != null)
            {
                WindowsPrincipal windowsPrincipal = new WindowsPrincipal(windowsIdentity);

                return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
            }
 
            return false;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요