Shut down hệ thống bằng C#

13 views
Skip to first unread message

k2 khmt3

unread,
Dec 27, 2009, 5:28:37 AM12/27/09
to nh3t.khmt3.k2
Đoạn mã sau đây dùng để shut down hệ điều hành sử dụng assembly
System.Management. Lưu ý nó sẽ không thực hiện được nếu trước đó đã có
phân quyền bảo mật. using System; using System.Collections.Generic;
using System.ComponentModel; using System.Data;...
Đoạn mã sau đây dùng để shut down hệ điều hành sử dụng assembly
System.Management. Lưu ý nó sẽ không thực hiện được nếu trước đó đã có
phân quyền bảo mật.
view plainprint?
1. using System;
2. using System.Collections.Generic;
3. using System.ComponentModel;
4. using System.Data;
5. using System.Drawing;
6. using System.Text;
7. using System.Windows.Forms;
8. // Remember to add a reference to the System.Management assembly
9. using System.Management;
10. namespace ShutDown
11. {
12. public partial class Form1 : Form
13. {
14. public Form1()
15. {
16. InitializeComponent();
17. }
18. private void btnShutDown_Click(object sender, EventArgs
e)
19. {
20. ManagementBaseObject mboShutdown = null;
21. ManagementClass mcWin32 = new ManagementClass
("Win32_OperatingSystem");
22. mcWin32.Get();
23. // You can't shutdown without security privileges
24. mcWin32.Scope.Options.EnablePrivileges = true;
25. ManagementBaseObject mboShutdownParams =
mcWin32.GetMethodParameters("Win32Shutdown");
26. // Flag 1 means we want to shut down the system
27. mboShutdownParams["Flags"] = "1";
28. mboShutdownParams["Reserved"] = "0";
29. foreach (ManagementObject manObj in
mcWin32.GetInstances())
30. {
31. mboShutdown = manObj.InvokeMethod("Win32Shutdown",
mboShutdownParams, null);
32. }
33. }
34. }
35. }

Reply all
Reply to author
Forward
0 new messages