资讯   |   开发   |   选机中心   |   产品大全 | IBM | 惠普 | 联想 | 戴尔 | 苹果 | 神舟
更多: | 华硕 | 明基 | 方正 | 紫光 | TCL | 夏新 | 联宝 | 宏碁 | 七喜 | 长城 | 清华同方 | 海尔 | 三星 | 东芝 | 索尼 | 富士通 | LG | 技术 | ddnoon
当前位置:笔记本 > 软件开发 >
Advertisement
文章正文

大侠看看修改系统时间的代码错在哪里!

类型:转载   责任编辑:asp.net   日期:2007/05/23


热门软件下载:


   

结构定义  
  [StructLayout   (LayoutKind.Sequential)]  
  public   struct   SYSTEMTIME  
  {  
  public   short   Year;  
  public   short   Month;  
  public   short   DayOfWeek;  
  public   short   Day;  
  public   short   Hour;  
  public   short   Minute;  
  public   short   Second;  
  public   short   Miliseconds;  
  }  
   
   
  api函数声明  
  [DllImport   ("kernel32.dll",   CharSet=CharSet.Ansi)]  
  public   extern   static   bool   SetSystemTime(ref   SYSTEMTIME   time);  
   
  调用代码  
  SYSTEMTIME   t   =   new   SYSTEMTIME   ();  
  t.Year   =   2000;   t.Month   =   1;   t.Day   =   2;   t.Hour   =   12;   t.Minute   =   5;  
  bool   r   =   SetSystemTime(ref   t);  
  Console.WriteLine(r.ToString());  
   
  调用后修改Hour错误,其它都正确,为什么!

网友回答:

发表者:iamygx

标准时间和本地时间没算好!  
  给你一个我用过的  
  using   System;  
  using   System.ComponentModel;  
  using   System.Management;  
  using   System.Net;  
  using   System.Runtime.InteropServices;  
  using   System.Text;  
   
  namespace   Gainer.Communal  
  {  
  ///   <summary>  
  ///   SystemMethod   的摘要说明。  
  ///   操作系统的一些方法  
  ///   </summary>  
   
  public   class   SystemMethod  
  {  
  //定义时间结构  
  public   struct   SystemTime    
  {  
  public   ushort   wYear;  
  public   ushort   wMonth;    
  public   ushort   wDayOfWeek;    
  public   ushort   wDay;    
  public   ushort   wHour;    
  public   ushort   wMinute;    
  public   ushort   wSecond;    
  public   ushort   wMilliseconds;    
  }  
   
  //导入SetSystemTime()  
  [DllImport("kernel32.dll",   SetLastError=true)]    
  static   extern   int   SetSystemTime   (ref   SystemTime   lpSystemTime);  
  [DllImport("kernel32.dll")]  
  private   static   extern   int   GetVolumeInformation(  
  string   lpRootPathName,  
  string   lpVolumeNameBuffer,  
  int   nVolumeNameSize,  
  ref   int   lpVolumeSerialNumber,  
  int   lpMaximumComponentLength,  
  int   lpFileSystemFlags,  
  string   lpFileSystemNameBuffer,  
  int   nFileSystemNameSize  
  );  
  static   SystemMethod()  
  {  
  //  
  //   TODO:   在此处添加构造函数逻辑  
  //  
  }  
   
  public   static   string   GetMacID()  
  {  
  ManagementClass   mc   =   new   ManagementClass("Win32_NetworkAdapterConfiguration");  
  ManagementObjectCollection   moc   =   mc.GetInstances();  
  string   macAddress   =   String.Empty;  
  foreach(ManagementObject   mo   in   moc)  
  {  
  if(macAddress   ==   String.Empty)     //   only   return   MAC   Address   from   first   card  
  {  
  if((bool)mo["IPEnabled"]   ==   true)    
  {  
  macAddress   =   mo["MacAddress"].ToString()   ;  
  mo.Dispose();  
  break;  
  }  
  }  
  mo.Dispose();  
  }  
   
  moc.Dispose();  
  mc.Dispose();  
  macAddress   =   macAddress.Replace(":",   "");  
  return   macAddress.ToString();  
  }  
   
  public   static   string   GetHDDID()  
  {  
  const   int   MAX_FILENAME_LEN   =   256;  
  int   retVal   =   0;  
  int   a   =0;  
  int   b   =0;  
  string   str1   =   null;  
  string   str2   =   null;  
  int   i   =   GetVolumeInformation(  
  "C:\\",  
  str1,  
  MAX_FILENAME_LEN,  
  ref   retVal,  
  a,  
  b,  
  str2,  
  MAX_FILENAME_LEN  
  );  
  return   retVal.ToString();  
  }  
  public   static   string   GetHDDID(string   drvID)  
  {  
  const   int   MAX_FILENAME_LEN   =   256;  
  int   retVal   =   0;  
  int   a   =0;  
  int   b   =0;  
  string   str1   =   null;  
  string   str2   =   null;  
  int   i   =   GetVolumeInformation(  
  drvID   +   @":\",  
  str1,  
  MAX_FILENAME_LEN,  
  ref   retVal,  
  a,  
  b,  
  str2,  
  MAX_FILENAME_LEN  
  );  
  return   retVal.ToString();  
  }  
  public   static   string   GetHostIP()  
  {  
  string   strIP;  
  try  
  {  
  strIP=Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString();  
  }  
  catch(Exception)  
  {  
  strIP="";  
  }  
  return   strIP;  
  }  
  public   static   string   GetHostName()  
  {  
  string   strHostName;  
  try  
  {  
  strHostName=Dns.GetHostName();  
  }  
  catch(Exception)  
  {  
  strHostName   =   "";  
  }  
  return   strHostName;  
  }  
  public   static   string   EncString(string   str)  
  {  
  return   str;  
  }  
   
  public   static   string   DecString(string   str)  
  {  
  return   str;  
  }  
  public   static   string   GetSerial()  
  {  
  string   strMacID   =   GetMacID();  
  return   EncString   (strMacID);  
  }  
   
  public   static   bool   SetLocalTime(DateTime   dtServer)  
  {  
  SystemTime   st=   new   SystemTime   (   )   ;  
  st.wYear=     (   ushort   )dtServer.Year   ;  
  st.wMonth=   (   ushort   )dtServer.Month   ;  
  st.wDayOfWeek=   (   ushort   )dtServer.DayOfWeek   ;  
  st.wDay=   (   ushort   )dtServer.Day   ;  
  st.wHour=Convert.ToUInt16   (   dtServer.Hour   )   ;  
  if   (   st.wHour>=12   )  
  {  
  st.wHour-=   (   ushort   )8   ;  
  }  
  else   if   (   st.wHour   >=   8   )  
  {  
  st.wHour-=   (   ushort   )8   ;  
  }  
  else  
  {  
  st.wHour+=   (   ushort   )16   ;  
  }  
  st.wMinute=Convert.ToUInt16   (   dtServer.Minute   )   ;  
  st.wSecond=Convert.ToUInt16   (   dtServer.Second   )   ;  
  st.wMilliseconds=Convert.ToUInt16   (   dtServer.Millisecond   )   ;  
  //修改本地端的时间和日期  
  if   (SetSystemTime   (ref   st   )!=0)  
  return   true;  
  else  
  return   false;  
  }  
  /*  
  用System.Windows.Forms名字空间下的Help类,它封装了HTMLHelp   1.0的引擎.  
  使用如下:  
  const   string   helpFile   =   "soapBroker.chm";  
  FileInfo   fi   =   new   FileInfo(helpFile);  
  if(fi.Exists)  
  {  
  if(sender   ==   mnuHelpContent)  
  {  
  Help.ShowHelp(this,   helpFile);  
  }  
  else   if(sender   ==   mnuHelpIndex)  
  {  
  Help.ShowHelpIndex(this,   helpFile);  
  }  
  else   if(sender   ==   mnuHelpSearch)  
  {  
  Help.ShowHelp(this,   helpFile,   HelpNavigator.Find,   "");  
  }  
  }  
  else  
  {  
  MessageBox.Show("未找到帮助文件!");  
  }  
  或者用System.Diagnostics   命名空间下的Process类启动一个新的进程。  
  Process.Start("YourHelp.chm");    
  */  
  }  
  }  
 

发表者:hbxtlhx

这个函数使用的是0时区的时间,对于我们用+8时区的,时间要自己算一下.  
 


 

 
热门推荐笔记本: 戴尔笔记本
相关文章:
笔记本相关:
IT技术文章:
webmaster:popbb@126.com   最佳浏览:1024X768 MSIE
©2007 popbb.net All Rights Reserved