当前位置:  -> 首页 -> 原创文章 -> 登陆/注销/重起电脑代码(C#)

上一篇 | 下一篇
登陆/注销/重起电脑代码(C#)
作者:洛羽叶  点击率:1464  发布时间:2006-12-14

Import function

 

       [ DllImport("user32") ]

       public static extern bool ExitWindowsEx(uint uFlags, uint dwReserved ) ;

 

Logon

 

       public bool Logon(string UserName,string Password)

       {                  

             

              //init variable

              string[] saTemp;

              string strUserName;

              string strDomainName;               

              RegistryKey rkTemp;                         

             

              try

              {    

                     //get user name and domain name

                     saTemp=UserName.Split("\".ToCharArray());                         

                     if(saTemp.Length>1)

                     {

                            strUserName=saTemp[1];

                            strDomainName=saTemp[0];

                     }

                     else

                     {

                            strUserName=saTemp[0];

                            strDomainName="";                                  

                     }

                    

                     //write logon information to registry                                

                     rkTemp=Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon",true);           

                     rkTemp.SetValue("AutoAdminLogon","1");

                     rkTemp.SetValue("AutoLogonCount",1);

                     rkTemp.SetValue("DefaultUserName",strUserName);

                     rkTemp.SetValue("DefaultPassword",Password);

                     rkTemp.SetValue("DefaultDomainName",strDomainName);

                     rkTemp.Close();                  

                           

                     //reboot

                     Reboot("");

                                  

                     return true;

              }

              catch(Exception exp)

              {

                     throw exp;

              }           

       }

 

LogOff

 

       ExitWindowsEx((uint)EWX.EWX_LOGOFF,0);

 

Reboot

 

       ExitWindowsEx((uint)(EWX.EWX_REBOOT | EWX.EWX_FORCE),0);

 

ShutDown

 

       ExitWindowsEx((uint)(EWX.EWX_SHUTDOWN | EWX.EWX_FORCE),0);

标签: C#
引用地址:本站原创
   站点首页      技术人生      旅途足迹      我要留言      友情链接      关于站长   
[本站统计]
在线人数:14
今日访问:653
总访问量:1936484
Copyright 2006-2022 EasyWeb 1.6 订阅 All Rights Reserved
粤ICP备08028977号-1
www.luoriver.com
Created by WWH in 2006