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. }