C# 实现自动关机程序带图形界面

需积分: 42 12 下载量 43 浏览量 更新于2024-08-02 1 收藏 76KB DOC 举报
"该资源提供了一段用于创建自动关机程序的代码,支持在Windows环境中运行。代码基于C#编程语言,使用了Windows Forms库来构建一个简单的图形用户界面。" 在标题和描述中提到的知识点是实现自动关机功能的程序设计。这通常涉及到操作系统级别的交互,例如设置定时任务或发送特定的系统命令。在这个案例中,开发者使用了C#语言,这是微软开发的一种面向对象的编程语言,特别适合于构建Windows应用。C#提供了丰富的类库,包括System.Diagnostics和System.Windows.Forms,使得与操作系统交互以及创建GUI变得简单。 代码中的关键部分包括: 1. `System.Diagnostics`: 这个命名空间包含处理进程、性能计数器和诊断工具的类。在自动关机程序中,可能使用了`Process`类或者`ProcessStartInfo`类来启动系统级别的命令,如`shutdown.exe`,来执行关机操作。 2. `System.Windows.Forms`: 这个命名空间包含了构建Windows桌面应用程序所需的控件和类。在描述中提到的图形界面,就是通过这个命名空间的类实现的,比如`Form`、`Label`、`ComboBox`、`DateTimePicker`等,它们构成了用户与程序交互的界面元素。 3. `DateTimePicker`: 这是一个让用户选择日期和时间的控件,用户可以通过它设定自动关机的具体时间。 4. `Button`和`CheckBox`: 分别代表按钮和复选框,可能是用来触发关机操作或设置附加选项的。 5. `using`语句: 这是用来导入命名空间的,方便后续代码中引用类和方法。 6. `private`和`public`: 这是访问修饰符,用于控制类成员的可见性,`private`表示只有类内部可以访问,而`public`则允许外部访问。 7. `class TimerComputerShutdown : System.Windows.Forms.Form`: 这定义了一个名为`TimerComputerShutdown`的类,它继承自`System.Windows.Forms.Form`,这意味着它是一个窗体类,可以作为应用程序的主窗口。 8. `ComboBox`可能用于提供不同的操作选项,比如关机、重启等。 9. `CheckBox`可能用于启用或禁用某些特性,例如立即关机或延迟关机。 这个程序通过C#和Windows Forms库构建了一个简单的用户界面,允许用户设置一个时间点,然后在指定的时间自动执行计算机的关机操作。这样的程序对于那些需要在特定时间自动关闭电脑的用户非常有用,比如在进行长时间下载或更新时。
2011-02-10 上传
C# 关机程序 收藏 1. using System; 2. using System.Runtime.InteropServices; 3. 4. class shoutdown{ 5. [StructLayout(LayoutKind.Sequential, Pack=1)] 6. internal struct TokPriv1Luid 7. { 8. public int Count; 9. public long Luid; 10. public int Attr; 11. } 12. 13. [DllImport("kernel32.dll", ExactSpelling=true) ] 14. internal static extern IntPtr GetCurrentProcess(); 15. 16. [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ] 17. internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok ); 18. 19. [DllImport("advapi32.dll", SetLastError=true) ] 20. internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid ); 21. 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. 26. [DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ] 27. internal static extern bool ExitWindowsEx( int flg, int rea ); 28. 29. internal const int SE_PRIVILEGE_ENABLED = 0x00000002; 30. internal const int TOKEN_QUERY = 0x00000008; 31. internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020; 32. internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege"; 33. internal const int EWX_LOGOFF = 0x00000000; 34. internal const int EWX_SHUTDOWN = 0x00000001; 35. internal const int EWX_REBOOT = 0x00000002; 36. internal const int EWX_FORCE = 0x00000004; 37. internal const int EWX_POWEROFF = 0x00000008; 38. internal const int EWX_FORCEIFHUNG = 0x00000010; 39. 40. private static void DoExitWin(int flg) 41. { 42. bool ok; 43. TokPriv1Luid tp; 44. IntPtr hproc = GetCurrentProcess(); 45. IntPtr htok = IntPtr.Zero; 46. ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok ); 47. tp.Count = 1; 48. tp.Luid = 0; 49. tp.Attr = SE_PRIVILEGE_ENABLED; 50. ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid ); 51. ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero ); 52. ok = ExitWindowsEx( flg, 0 ); 53. } 54. 55. public static void Main() 56. { 57. Console.WriteLine("正在关机……"); 58. // 修改 EWX_SHUTDOWN 或者 EWX_LOGOFF, EWX_REBOOT等实现不同得功能。 59. // 在XP下可以看到帮助信息,以得到不同得参数 60. // SHUTDOWN /? 61. DoExitWin(EWX_SHUTDOWN); 62. } 63. } 64. 65.