用c#代码怎么实现XP系统的开关机???

解决方案 »

  1.   


    using System.Runtime.InteropServices;//用来调用API函数
    using System.Management;//调用API   
    //==============**********调用API*************
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    internal struct TokPriv1Luid
    {
    public int Count;
    public long Luid;
    public int Attr;
    } [DllImport("kernel32.dll", ExactSpelling = true)]
    internal static extern IntPtr GetCurrentProcess(); [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
    internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok); [DllImport("advapi32.dll", SetLastError = true)]
    internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid); [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
    internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
    ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen); [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
    internal static extern bool ExitWindowsEx(int flg, int rea); internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
    internal const int TOKEN_QUERY = 0x00000008;
    internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
    internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
    internal const int EWX_LOGOFF = 0x00000000;
    internal const int EWX_SHUTDOWN = 0x00000001;
    internal const int EWX_REBOOT = 0x00000002;
    internal const int EWX_FORCE = 0x00000004;
    internal const int EWX_POWEROFF = 0x00000008;
    internal const int EWX_FORCEIFHUNG = 0x00000010; private bool DoExitWin(int flg)
    {
    bool ok;
    TokPriv1Luid tp;
    IntPtr hproc = GetCurrentProcess();
    IntPtr htok = IntPtr.Zero;
    ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
    tp.Count = 1;
    tp.Luid = 0;
    tp.Attr = SE_PRIVILEGE_ENABLED;
    ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
    ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
    ok = ExitWindowsEx(flg, 0);
    return ok;
    }
    /// <summary>
    /// 注销计算机
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button47_Click(object sender, System.EventArgs e)
    {
    DoExitWin(EWX_LOGOFF);
    }
    /// <summary>
    /// 关闭计算机
    /// </summary>
    private void button46_Click(object sender, System.EventArgs e)
    {
    DoExitWin(EWX_SHUTDOWN);
    }

    /// <summary>
    /// 取消操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button43_Click(object sender, System.EventArgs e)
    {
    this.Close();
    }

    /// <summary>
    /// 重启计算机
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button45_Click(object sender, System.EventArgs e)
    {
    DoExitWin(EWX_REBOOT); 
    }
      

  2.   

    如果有权限的话,直接用rundll32.exe调用系统命令就可以了
    http://blog.csdn.net/jinjazz/archive/2008/04/17/2302095.aspx如果没有权限需要先用windows api函数提升关机权限
      

  3.   

    http://blog.csdn.net/null1/archive/2008/11/03/3208145.aspx
      

  4.   

    Process 一个 shutdown -s -t 0 就行了~~开机呀楼主~~这个没想过,关机以后恐怕 .Net Runtime 就没了,要实现开机的话,起码需要一台服务器,做网络唤醒才行
      

  5.   

    关机可以用这个,
    System.diagnostics.process=new system.diagnostics.process();
    myprocess.startinfo.filename="cmd.exe";
    myprocess.startinfo.useshellexecute=false;
    myprocess.startinfo.Redirectstandardlnput=true;
    myprocess.startinfo.Redirectstandardoutput=true;
    myprocess.startinfo.RedirectstandardError=true;
    myprocess.startinfo.Createnowindow=true;
    myprocess.start();
    myprocess.standardlnput.writeline("shoutdown-s-t 0");
    system.diagnostics.process.start("cmd.exe","shutdown-s-t%time%");
    -s关机
    -l注销
    -r重启
    -t+时间
    shutdown+你要干什么+-t你要执行的时间