13. 5. 14.

C# 컴퓨터 재부팅 및 끄기

프로젝트 << 참조 << .NET << System.Management 추가
나머지는 알아서 추가 ㅡ.ㅡ;;


public partial class Form1 : Form
    {
        [DllImport("user32.Dll")]

        public static extern int SystemParametersInfo(int uAction, int uparam, string lpvParam, int fuWinIni);

        public Form1()
        {
            InitializeComponent();
        }

        public enum ShutDown
        {
            LogOff = 0,
            Shutdown = 1,
            Reboot = 2,
            ForcedLogOff = 4,
            ForcedShutdown = 5,
            ForcedReboot = 6,
            PowerOff = 8,
            ForcedPowerOff = 12
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 컴퓨터 재시작
            // shutDown(ShutDown.ForcedReboot);
            // 컴퓨터 끄기
            // shutDown(ShutDown.ForcedShutdown);
        }

        // 끄기모드
        private void shutDown(ShutDown flag)
        {
            ManagementBaseObject outParam = null;
            ManagementClass sysOS = new ManagementClass("Win32_OperatingSystem");
            sysOS.Get();
            // enables required security privilege.
            sysOS.Scope.Options.EnablePrivileges = true;
            // get our in parameters
            ManagementBaseObject inParams = sysOS.GetMethodParameters("Win32Shutdown");
            // pass the flag of 0 = System Shutdown
            inParams["Flags"] = flag;
            inParams["Reserved"] = "0";
            foreach (ManagementObject manObj in sysOS.GetInstances())
            {
                outParam = manObj.InvokeMethod("Win32Shutdown", inParams, null);
            }
        }
    }

댓글 없음:

댓글 쓰기