1
public
class
SysTimeZoneCheck
2![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
4
/// [Win32 API call]
5
/// The GetTimeZoneInformation function retrieves the current time-zone parameters.
6
/// These parameters control the translations between Coordinated Universal Time (UTC)
7
/// and local time.
8
/// </summary>
9
/// <param name="lpTimeZoneInformation">[out] Pointer to a TIME_ZONE_INFORMATION structure to receive the current time-zone parameters.</param>
10
/// <returns>
11
/// If the function succeeds, the return value is one of the following values.
12
/// <list type="table">
13
/// <listheader>
14
/// <term>Return code/value</term>
15
/// <description>Description</description>
16
/// </listheader>
17
/// <item>
18
/// <term>TIME_ZONE_ID_UNKNOWN == 0</term>
19
/// <description>
20
/// The system cannot determine the current time zone. This error is also returned if you call the SetTimeZoneInformation function and supply the bias values but no transition dates.
21
/// This value is returned if daylight saving time is not used in the current time zone, because there are no transition dates.
22
/// </description>
23
/// </item>
24
/// <item>
25
/// <term>TIME_ZONE_ID_STANDARD == 1</term>
26
/// <description>
27
/// The system is operating in the range covered by the StandardDate member of the TIME_ZONE_INFORMATION structure.
28
/// </description>
29
/// </item>
30
/// <item>
31
/// <term>TIME_ZONE_ID_DAYLIGHT == 2</term>
32
/// <description>
33
/// The system is operating in the range covered by the DaylightDate member of the TIME_ZONE_INFORMATION structure.
34
/// </description>
35
/// </item>
36
/// </list>
37
/// If the function fails, the return value is TIME_ZONE_ID_INVALID. To get extended error information, call GetLastError.
38
/// </returns>
39
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
40
private static extern int GetTimeZoneInformation(out TimeZoneInformation lpTimeZoneInformation);
41![](/Images/OutliningIndicators/InBlock.gif)
42![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
43
/// [Win32 API call]
44
/// The SetTimeZoneInformation function sets the current time-zone parameters.
45
/// These parameters control translations from Coordinated Universal Time (UTC)
46
/// to local time.
47
/// </summary>
48
/// <param name="lpTimeZoneInformation">[in] Pointer to a TIME_ZONE_INFORMATION structure that contains the time-zone parameters to set.</param>
49
/// <returns>
50
/// If the function succeeds, the return value is nonzero.
51
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
52
/// </returns>
53
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
54
private static extern bool SetTimeZoneInformation([In] ref TimeZoneInformation lpTimeZoneInformation);
55![](/Images/OutliningIndicators/InBlock.gif)
56![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
57
/// The SystemTime structure represents a date and time using individual members
58
/// for the month, day, year, weekday, hour, minute, second, and millisecond.
59
/// </summary>
60
[StructLayoutAttribute(LayoutKind.Sequential)]
61
private struct SystemTime
62![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
63
public short year;
64
public short month;
65
public short dayOfWeek;
66
public short day;
67
public short hour;
68
public short minute;
69
public short second;
70
public short milliseconds;
71
}
72![](/Images/OutliningIndicators/InBlock.gif)
73![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
74
/// The TimeZoneInformation structure specifies information specific to the time zone.
75
/// </summary>
76
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
77
private struct TimeZoneInformation
78![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
79![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
80
/// Current bias for local time translation on this computer, in minutes. The bias is the difference, in minutes, between Coordinated Universal Time (UTC) and local time. All translations between UTC and local time are based on the following formula:
81
/// <para>UTC = local time + bias</para>
82
/// <para>This member is required.</para>
83
/// </summary>
84
public int bias;
85![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
86
/// Pointer to a null-terminated string associated with standard time. For example, "EST" could indicate Eastern Standard Time. The string will be returned unchanged by the GetTimeZoneInformation function. This string can be empty.
87
/// </summary>
88
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
89
public string standardName;
90![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
91
/// A SystemTime structure that contains a date and local time when the transition from daylight saving time to standard time occurs on this operating system. If the time zone does not support daylight saving time or if the caller needs to disable daylight saving time, the wMonth member in the SystemTime structure must be zero. If this date is specified, the DaylightDate value in the TimeZoneInformation structure must also be specified. Otherwise, the system assumes the time zone data is invalid and no changes will be applied.
92
/// <para>To select the correct day in the month, set the wYear member to zero, the wHour and wMinute members to the transition time, the wDayOfWeek member to the appropriate weekday, and the wDay member to indicate the occurence of the day of the week within the month (first through fifth).</para>
93
/// <para>Using this notation, specify the 2:00a.m. on the first Sunday in April as follows: wHour = 2, wMonth = 4, wDayOfWeek = 0, wDay = 1. Specify 2:00a.m. on the last Thursday in October as follows: wHour = 2, wMonth = 10, wDayOfWeek = 4, wDay = 5.</para>
94
/// </summary>
95
public SystemTime standardDate;
96![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
97
/// Bias value to be used during local time translations that occur during standard time. This member is ignored if a value for the StandardDate member is not supplied.
98
/// <para>This value is added to the value of the Bias member to form the bias used during standard time. In most time zones, the value of this member is zero.</para>
99
/// </summary>
100
public int standardBias;
101![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
102
/// Pointer to a null-terminated string associated with daylight saving time. For example, "PDT" could indicate Pacific Daylight Time. The string will be returned unchanged by the GetTimeZoneInformation function. This string can be empty.
103
/// </summary>
104
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
105
public string daylightName;
106![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
107
/// A SystemTime structure that contains a date and local time when the transition from standard time to daylight saving time occurs on this operating system. If the time zone does not support daylight saving time or if the caller needs to disable daylight saving time, the wMonth member in the SystemTime structure must be zero. If this date is specified, the StandardDate value in the TimeZoneInformation structure must also be specified. Otherwise, the system assumes the time zone data is invalid and no changes will be applied.
108
/// <para>To select the correct day in the month, set the wYear member to zero, the wHour and wMinute members to the transition time, the wDayOfWeek member to the appropriate weekday, and the wDay member to indicate the occurence of the day of the week within the month (first through fifth).</para>
109
/// </summary>
110
public SystemTime daylightDate;
111![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
112
/// Bias value to be used during local time translations that occur during daylight saving time. This member is ignored if a value for the DaylightDate member is not supplied.
113
/// <para>This value is added to the value of the Bias member to form the bias used during daylight saving time. In most time zones, the value of this member is –60.</para>
114
/// </summary>
115
public int daylightBias;
116
}
117![](/Images/OutliningIndicators/InBlock.gif)
118![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
119
/// Sets new time-zone information for the local system.
120
/// </summary>
121
/// <param name="tzi">Struct containing the time-zone parameters to set.</param>
122
private static void SetTimeZone(TimeZoneInformation tzi)
123![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
124
// set local system timezone
125
SetTimeZoneInformation(ref tzi);
126
}
127![](/Images/OutliningIndicators/InBlock.gif)
128![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
129
/// Gets current timezone information for the local system.
130
/// </summary>
131
/// <returns>Struct containing the current time-zone parameters.</returns>
132
private static TimeZoneInformation GetTimeZone()
133![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
134
// create struct instance
135
TimeZoneInformation tzi;
136![](/Images/OutliningIndicators/InBlock.gif)
137
// retrieve timezone info
138
int currentTimeZone = GetTimeZoneInformation(out tzi);
139![](/Images/OutliningIndicators/InBlock.gif)
140
return tzi;
141
}
142![](/Images/OutliningIndicators/InBlock.gif)
143
public static bool TimeZoneCheck()
144![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
145
if (GetTimeZone().bias != -480)
146![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
147
TimeZoneInformation tzi=new TimeZoneInformation();
148
tzi.bias=-480;
149
tzi.standardName = "China Standard Time";
150
tzi.daylightName = "China Standard Time";
151
SetTimeZone(tzi);
152
return false;
153
}
154
return true;
155
}
156
}
157
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
3
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/InBlock.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
79
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
88
![](/Images/OutliningIndicators/InBlock.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
95
![](/Images/OutliningIndicators/InBlock.gif)
96
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
102
![](/Images/OutliningIndicators/InBlock.gif)
103
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
104
![](/Images/OutliningIndicators/InBlock.gif)
105
![](/Images/OutliningIndicators/InBlock.gif)
106
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
107
![](/Images/OutliningIndicators/InBlock.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
110
![](/Images/OutliningIndicators/InBlock.gif)
111
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/InBlock.gif)
114
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
115
![](/Images/OutliningIndicators/InBlock.gif)
116
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
117
![](/Images/OutliningIndicators/InBlock.gif)
118
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
119
![](/Images/OutliningIndicators/InBlock.gif)
120
![](/Images/OutliningIndicators/InBlock.gif)
121
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
122
![](/Images/OutliningIndicators/InBlock.gif)
123
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
124
![](/Images/OutliningIndicators/InBlock.gif)
125
![](/Images/OutliningIndicators/InBlock.gif)
126
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
127
![](/Images/OutliningIndicators/InBlock.gif)
128
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
129
![](/Images/OutliningIndicators/InBlock.gif)
130
![](/Images/OutliningIndicators/InBlock.gif)
131
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
132
![](/Images/OutliningIndicators/InBlock.gif)
133
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
134
![](/Images/OutliningIndicators/InBlock.gif)
135
![](/Images/OutliningIndicators/InBlock.gif)
136
![](/Images/OutliningIndicators/InBlock.gif)
137
![](/Images/OutliningIndicators/InBlock.gif)
138
![](/Images/OutliningIndicators/InBlock.gif)
139
![](/Images/OutliningIndicators/InBlock.gif)
140
![](/Images/OutliningIndicators/InBlock.gif)
141
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
142
![](/Images/OutliningIndicators/InBlock.gif)
143
![](/Images/OutliningIndicators/InBlock.gif)
144
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
145
![](/Images/OutliningIndicators/InBlock.gif)
146
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
147
![](/Images/OutliningIndicators/InBlock.gif)
148
![](/Images/OutliningIndicators/InBlock.gif)
149
![](/Images/OutliningIndicators/InBlock.gif)
150
![](/Images/OutliningIndicators/InBlock.gif)
151
![](/Images/OutliningIndicators/InBlock.gif)
152
![](/Images/OutliningIndicators/InBlock.gif)
153
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
154
![](/Images/OutliningIndicators/InBlock.gif)
155
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
156
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
157
![](/Images/OutliningIndicators/None.gif)
在修改系统时区为北京时区后,应该重启电脑生效。附重启电脑程序代码:如下:
1
/**/
/// 关机调用API程序 参数是修改 flg为 EWX_SHUTDOWN(关机)或者 EWX_LOGOFF(注销), EWX_REBOOT(重启)
2
/// </summary>
3
public
class
Shoutdown
4![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
5
[StructLayout(LayoutKind.Sequential, Pack = 1)]
6
internal struct TokPriv1Luid
7![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
public int Count;
9
public long Luid;
10
public int Attr;
11
}
12![](/Images/OutliningIndicators/InBlock.gif)
13
[DllImport("kernel32.dll", ExactSpelling = true)]
14
internal static extern IntPtr GetCurrentProcess();
15![](/Images/OutliningIndicators/InBlock.gif)
16
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
17
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
18![](/Images/OutliningIndicators/InBlock.gif)
19
[DllImport("advapi32.dll", SetLastError = true)]
20
internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
21![](/Images/OutliningIndicators/InBlock.gif)
22
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
23
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
24
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
25![](/Images/OutliningIndicators/InBlock.gif)
26
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
27
internal static extern bool ExitWindowsEx(int flg, int rea);
28![](/Images/OutliningIndicators/InBlock.gif)
29
public const int SE_PRIVILEGE_ENABLED = 0x00000002;
30
public const int TOKEN_QUERY = 0x00000008;
31
public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
32
public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
33
public const int EWX_LOGOFF = 0x00000000;
34
public const int EWX_SHUTDOWN = 0x00000001;
35
public const int EWX_REBOOT = 0x00000002;
36
public const int EWX_FORCE = 0x00000004;
37
public const int EWX_POWEROFF = 0x00000008;
38
public const int EWX_FORCEIFHUNG = 0x00000010;
39![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
40
/// 应用时调用下面的程序
41
/// 参数是修改 flg为 EWX_SHUTDOWN(关机)或者 EWX_LOGOFF(注销), EWX_REBOOT(重启)等实现不同功能
42
/// </summary>
43
/// <param name="flg"></param>
44
public void DoExitWin(int flg)
45![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
bool ok;
47
TokPriv1Luid tp;
48
IntPtr hproc = GetCurrentProcess();
49
IntPtr htok = IntPtr.Zero;
50
ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
51
tp.Count = 1;
52
tp.Luid = 0;
53
tp.Attr = SE_PRIVILEGE_ENABLED;
54
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
55
ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
56
ok = ExitWindowsEx(flg, 0);
57
}
58
}
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
58
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
调用:
1
2
Shoutdown shutDown
=
new
Shoutdown();
3
shutDown.DoExitWin(Shoutdown.EWX_SHUTDOWN);
4![](/Images/OutliningIndicators/None.gif)
5
6
Application.Exit();
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)