winfrom 获取当前系统时间


public class LibWrapDateTime
		{
			[DllImportAttribute( "Kernel32.dll" )]
			public static extern void GetLocalTime(SystemTime st );
			[DllImportAttribute( "Kernel32.dll" )]
			public static extern void SetLocalTime(SystemTime st );
		}
		[StructLayoutAttribute( LayoutKind.Sequential)]

public class 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; 
		}
		private void button1_Click(object sender, System.EventArgs e)
		{//获取当前系统日期时间			
		  DateTimePicker OlddateTimePicker = new DateTimePicker();		  
		  this.textBox1.Text=OlddateTimePicker.Value.ToLongDateString().ToString();
          this.textBox1.Text+=OlddateTimePicker.Value.ToLongTimeString().ToString();		
		 }

		private void button2_Click(object sender, System.EventArgs e)
		{//设置系统当前日期时间
			if(MessageBox.Show("您真的确定更改系统当前日期时间吗?","信息提示",MessageBoxButtons.OK)==DialogResult.OK)
			{
				DateTime Year=this.dateTimePicker1.Value;
				SystemTime MySystemTime = new SystemTime();
				LibWrapDateTime.GetLocalTime(MySystemTime);
				MySystemTime.wYear=(ushort)this.dateTimePicker1.Value.Year;
				MySystemTime.wMonth=(ushort)this.dateTimePicker1.Value.Month;
				MySystemTime.wDay=(ushort)this.dateTimePicker1.Value.Day;
				MySystemTime.wHour=(ushort)this.dateTimePicker2.Value.Hour;
				MySystemTime.wMinute=(ushort)this.dateTimePicker2.Value.Minute;
				MySystemTime.wSecond=(ushort)this.dateTimePicker2.Value.Second;
				LibWrapDateTime.SetLocalTime(MySystemTime);
				button1_Click(null,null);
			}
		}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM