Tải bản đầy đủ (.doc) (2 trang)

Tài liệu C Thiết lập ngày giờ hệ thống ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (68.74 KB, 2 trang )

C Thiết lập ngày giờ hệ thống
Cập nhật: 26/8/2008 với no comments
Đoạn mã C# code snippet sau đây sử dụng mã không quản để lấy về thời gian hiện tại của
hệ điều hành Windows, đồng thời cũng cho phép bạn thiết lập lại thời gian cụ thể nào đó
tùy ý. using System; using System.Collections.Generic; using
System.ComponentModel;
Đoạn mã C# code snippet sau đây sử dụng mã không quản để lấy về thời gian hiện tại của
hệ điều hành Windows, đồng thời cũng cho phép bạn thiết lập lại thời gian cụ thể nào đó
tùy ý.
view plain print ?
1.
2. using System;
3. using System.Collections.Generic;
4. using System.ComponentModel;
5. using System.Data;
6. using System.Windows.Forms;
7. using System.Runtime.InteropServices;
8. namespace Sample
9. {
10. public partial class Form1 : Form
11. {
12. public Form1()
13. {
14. InitializeComponent();
15. }
16. public struct SystemTime
17. {
18. public ushort Year;
19. public ushort Month;
20. public ushort DayOfWeek;
21. public ushort Day;


22. public ushort Hour;
23. public ushort Minute;
24. public ushort Second;
25. public ushort Millisecond;
26. };
27. [DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = tr
ue)]
28. public extern static void Win32GetSystemTime(ref SystemTime sysTime);
29. [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = tr
ue)]
30. public extern static bool Win32SetSystemTime(ref SystemTime sysTime);
31. private void button1_Click(object sender, EventArgs e)
32. {
33. // Set system date and time
34. SystemTime updatedTime = new SystemTime();
35. updatedTime.Year = (ushort)2008;
36. updatedTime.Month = (ushort)4;
37. updatedTime.Day = (ushort)23;
38. // UTC time; it will be modified according to the regional settings of the ta
rget computer so the actual hour might differ
39. updatedTime.Hour = (ushort)10;
40. updatedTime.Minute = (ushort)0;
41. updatedTime.Second = (ushort)0;
42. // Call the unmanaged function that sets the new date and time instantly
43. Win32SetSystemTime(ref updatedTime);
44. // Retrieve the current system date and time
45. SystemTime currTime = new SystemTime();
46. Win32GetSystemTime(ref currTime);
47. // You can now use the struct to retrieve the date and time
48. MessageBox.Show("It's " + currTime.Hour + " o'clock. Do you know whe

re your C# code is?");
49. }
50. }
51. }

×