当前位置:首页
开发技术指南» 文章正文
    引言:

    摘要: 我是南京航空航天大学的一名学生,今年本科毕业找工作,想去深圳,大家帮忙看以下,请大侠鉴定下~~~ 专业 计算机科学与技术 技术特长及个人特点 1----熟悉c, c++语言,oo思想,bcb编译器和vcl构架,ms sqlserver数据库,asp,ado,tcp/ip协议,编程功底良好。 2----了解vc,mfc,.net基本架构及类库,ado.net,c#,asp.net,orac......
    摘要: 先设立状态为"保护",2小时候后如果保护状态还没有修改的话,自动取消保护状态还原以前的状态!请问要怎么做?谢谢 ......


用C#如何来控制系统关机

请教用C#如何来控制系统关机  
   
   
  问题是这样的,,   我已经将系统的关机给屏蔽了,,,但我要关机,所以我用C#自制一个关机程序.  
   
  请问我怎样才能获取系统的关机的程序呢??在   win   2000   /xp   /2003   三种系统下面.      
   
 

NO.1   作者: weisunding

public   enum   ExitWindows   :   uint  
  {  
        LogOff   =   0x00,  
        ShutDown   =   0x01,  
        Reboot   =   0x02,  
        Force   =   0x04,  
        PowerOff   =   0x08,  
        ForceIfHung   =   0x10  
  }  
   
   
   
  [DllImport("user32.dll")]  
                  static   extern   bool   ExitWindowsEx(ExitWindows   uFlags,   ShutdownReason   dwReason);  
   
   
                  [STAThread]  
                  static   void   Main(string[]   args)  
                  {  
                          ExitWindowsEx(ExitWindows.LogOff,   ShutdownReason.MajorOther   &   ShutdownReason.MinorOther);    
                          //this   will   cause   the   computer   to   logoff.  
                  }  
 

NO.2   作者: jinjazz

在2000下的话需要提升关机权限  
   
  using   System;  
  using   System.Runtime.InteropServices;  
   
  namespace   MDINew  
  {  
   
  public   class   Class1  
  {  
  [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   flag,   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   void   DoExitWin(   int   flag   )  
  {  
  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(   flag,   0   );  
  }  
   
  public   void   Reboot()  
  {  
  DoExitWin(   EWX_REBOOT   );  
  }  
   
   
   
  }  
  }  
 

NO.3   作者: tashanzhishi

这个我的自动关机的几个类,非常容易调用:  
  using   System;     
  using   System.Collections;     
  using   System.Runtime.InteropServices;     
  using   System.Security.Principal;  
   
   
  namespace   closeauto  
  {  
  [StructLayout(LayoutKind.Sequential,   CharSet=CharSet.Auto)]    
  public   struct   LUID    
  {  
  public   uint   LowPart;    
  public   uint   HighPart;    
  }  
   
  [StructLayout(LayoutKind.Sequential,   CharSet=CharSet.Auto)]    
  public   struct   TOKEN_PRIVILEGES  
  {    
  public   uint   PrivilegeCount;    
  public   LUID   Luid;    
  public   uint   Attributes;    
  }  
   
  public   class   Win32API  
  {  
  [DllImport("Advapi32",   CharSet=CharSet.Auto)]    
  public   static   extern   bool   LookupPrivilegeValue   (string   sysname,string   privname,ref   LUID   luid);    
   
  [DllImport("advapi32",   CharSet=CharSet.Auto)]    
  public   static   extern   bool   AdjustTokenPrivileges(IntPtr   handle,   bool   dsall,ref   TOKEN_PRIVILEGES   newstate,int   len,   IntPtr   oldstate,IntPtr   retlen);    
   
  [DllImport("kernel32.dll")]     
  public   static   extern   int   GetLastError();     
   
  [DllImport("user32.dll")]  
  public   static   extern   bool   ExitWindowsEx(int   uFlags,   int   dwReason);     
   
  }    
   
  public   class   PowerFlag  
  {  
  ///   <summary>  
  ///   注销当前用户  
  ///   </summary>  
  public   static   readonly   int   EWX_LOGOFF=0;  
  ///   <summary>  
  ///   关闭计算机  
  ///   </summary>  
  public   static   readonly   int   EWX_SHUTDOWN=1;  
  ///   <summary>  
  ///   重新启动计算机  
  ///   </summary>  
  public   static   readonly   int   EWX_REBOOT=2;  
  ///   <summary>  
  ///   强制终止所有的进程  
  ///   </summary>  
  public   static   readonly   int   EWX_FORCE=4 ;  
  ///   <summary>  
  ///   关闭电源  
  ///   </summary>  
  public   static   readonly   int   EWX_POWEROFF   =   8   ;  
   
  }  
   
  ///   <summary>  
  ///   PowerOff   的摘要说明。  
  ///   </summary>  
  public   class   PowerOff  
  {  
  public   PowerOff()  
  {  
  //  
  //   TODO:   在此处添加构造函数逻辑  
  //  
  }  
  public   static   bool   ForceToClose   =   false   ;  
   
  public   static   bool   ReSet()  
  {  
  return   ExitWindow(PowerFlag.EWX_REBOOT+PowerFlag.EWX_LOGOFF)   ;  
  }  
  public   static   bool   ShutDown()  
  {  
  return   ExitWindow(PowerFlag.EWX_POWEROFF+PowerFlag.EWX_LOGOFF)   ;  
  }  
  public   static   bool   ReLogin()  
  {  
  return   ExitWindow(PowerFlag.EWX_LOGOFF)   ;  
  }  
   
  private     static   bool   ExitWindow(int   iFlag)  
  {  
  WindowsIdentity   identity   =   WindowsIdentity.GetCurrent();  
  IntPtr   token   =   identity.Token;    
  IntPtr   luid   =   (IntPtr)0;    
  IntPtr   previousState   =   (IntPtr)0;    
  IntPtr   previousStateLength   =   (IntPtr)0;  
  LUID   privilegeId   =   new   LUID   ();  
  Win32API.LookupPrivilegeValue   ("",   "SeShutdownPrivilege",   ref   privilegeId);  
  TOKEN_PRIVILEGES   privileges   =   new   TOKEN_PRIVILEGES();    
  privileges.PrivilegeCount   =   1;  
  privileges.Luid   =   privilegeId;  
  privileges.Attributes   =   2;  
  Win32API.AdjustTokenPrivileges(token,false,   ref   privileges,   Marshal.SizeOf(privileges),   previousState,   previousStateLength);    
  if   (Win32API.GetLastError()   !=   0)   return   false   ;  
   
  if   (ForceToClose)  
  iFlag   =   iFlag   +   PowerFlag.EWX_FORCE   ;  
   
  if(Win32API.ExitWindowsEx(iFlag,   0))   return   false   ;  
   
  return   true   ;  
  }  
  }//end   class  
  }//end   namespace


    摘要: 程序中datagrid有“下载文档”一列,在该列放了三个hyperlink控件,分别为“中”、“英”、“法”,链接到该行记录所对应的中文附件、英文附件、法文附件的显示页面,代码如下所示: <asp:templatecolumn headertext="下载文档"> <itemstyle width="15%"></ite......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE