求在BUTTONCLIKE中关闭计算机的代码

解决方案 »

  1.   

    using System.Runtime.InteropServices; //加入此名字间是为了引用Windows API来实现关机 
    //---------------关机部分程序------------------------
    private void ShutDown_Click(object sender, System.EventArgs e)
    {if(DialogResult.OK == MessageBox.Show("Are you sure want to Shut Down?","Confirm",MessageBoxButtons.OKCancel,MessageBoxIcon.Information)) //同上
    {//主要调用系统API来实现
    ExitWindowsEx(1,0);
    }
    }
    //这里的[DllImport("user32.dll")]是.net里属性的表达方法,DllImport为引用系统API所在的库"user32.dll"[DllImport("user32.dll")]
    //主要API是这个,注意:必须声明为static extern
    private static extern int ExitWindowsEx(int x,int y);