知识大全 使用C#编写的一个定时关机程序

Posted 时间

篇首语:书籍是横渡时间大海的航船。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 使用C#编写的一个定时关机程序相关的知识,希望对你有一定的参考价值。

使用C#编写的一个定时关机程序  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  有很多的软件都实现了自动关机这样的功能 前一阵在Codeproject上看到一个这样的程序 整理了一下共享给大家 里面实现了关机 重启 注销等功能

using System;using System Collections;using System ComponentModel;using System Drawing;using System Diagnostics;using System Runtime InteropServices;using System Threading;using System Windows Forms;

namespace TimerComputerShutdown/// <summary>/// Summary description for TimerComputerShutdown /// </summary>public class TimerComputerShutdown : System Windows Forms Formprivate System Windows Forms Label labelWhatDoYouWantTheComputerToDo;private System Windows Forms ComboBox boBox;private System Windows Forms Label labelSelectDateTimeToShutdownComputer;private System Windows Forms DateTimePicker dateTimePicker;private System Windows Forms Label labelDateTimeNow;private System Windows Forms Label labelDateTimeShutdown;private System Windows Forms Label labelNow;private System Windows Forms Label labelShutdown;private System Windows Forms Panel panelParent;private System Windows Forms Panel panelChild;private System Windows Forms CheckBox checkBox; private System Windows Forms Button buttonCancel;private System Windows Forms Button buttonInvoke;/// <summary>/// Required designer variable /// </summary>private System ComponentModel Container ponents = null;private System Threading ThreadStart threadStart;private System Threading Thread thread;

[DllImport( user dll )] private static extern bool SetForegroundWindow(IntPtr hWnd);[DllImport( user dll )] private static extern bool ShowWindowAsync(IntPtr hWnd int nCmdShow);[DllImport( user dll )] private static extern bool IsIconic(IntPtr hWnd);

private const int SW_HIDE = ;private const int SW_SHOWNORMAL = ;private const int SW_SHOWMINIMIZED = ;private const int SW_SHOWMAXIMIZED = ;private const int SW_SHOWNOACTIVATE = ;private const int SW_RESTORE = ;private const int SW_SHOWDEFAULT = ;public TimerComputerShutdown()InitializeComponent();

/// <summary>/// Clean up any resources being used /// </summary>protected override void Dispose( bool disposing )if( disposing )if (ponents != null) ponents Dispose();base Dispose( disposing );

#region Windows Form Designer generated code/// <summary>/// Required method for Designer support do not modify/// the contents of this method with the code editor /// </summary>private void InitializeComponent()System Resources ResourceManager resources = new System Resources ResourceManager(typeof(TimerComputerShutdown));this labelWhatDoYouWantTheComputerToDo = new System Windows Forms Label();this boBox = new System Windows Forms ComboBox();this labelSelectDateTimeToShutdownComputer = new System Windows Forms Label();this dateTimePicker = new System Windows Forms DateTimePicker();this labelDateTimeNow = new System Windows Forms Label();this labelDateTimeShutdown = new System Windows Forms Label();this labelNow = new System Windows Forms Label();this labelShutdown = new System Windows Forms Label();this panelParent = new System Windows Forms Panel();this panelChild = new System Windows Forms Panel();this buttonCancel = new System Windows Forms Button();this buttonInvoke = new System Windows Forms Button();this checkBox = new System Windows Forms CheckBox();this panelParent SuspendLayout();this panelChild SuspendLayout();this SuspendLayout();// // labelWhatDoYouWantTheComputerToDo// this labelWhatDoYouWantTheComputerToDo Location = new System Drawing Point( );this labelWhatDoYouWantTheComputerToDo Name = labelWhatDoYouWantTheComputerToDo ;this labelWhatDoYouWantTheComputerToDo Size = new System Drawing Size( );this labelWhatDoYouWantTheComputerToDo TabIndex = ;this labelWhatDoYouWantTheComputerToDo Text = 选择任务: ;this labelWhatDoYouWantTheComputerToDo TextAlign = System Drawing ContentAlignment MiddleLeft;// // boBox// this boBox DropDownStyle = System Windows Forms ComboBoxStyle DropDownList;this boBox Items AddRange(new object[] 关机 重启 待机 睡眠 注销 );this boBox Location = new System Drawing Point( );this boBox Name = boBox ;this boBox Size = new System Drawing Size( );this boBox TabIndex = ;this boBox SelectedIndexChanged += new System EventHandler(this boBox_SelectedIndexChanged);// // labelSelectDateTimeToShutdownComputer// this labelSelectDateTimeToShutdownComputer Location = new System Drawing Point( );this labelSelectDateTimeToShutdownComputer Name = labelSelectDateTimeToShutdownComputer ;this labelSelectDateTimeToShutdownComputer Size = new System Drawing Size( );this labelSelectDateTimeToShutdownComputer TabIndex = ;this labelSelectDateTimeToShutdownComputer Text = 选择任务时间: ;this labelSelectDateTimeToShutdownComputer TextAlign = System Drawing ContentAlignment MiddleLeft;// // dateTimePicker// this dateTimePicker CalendarFont = new System Drawing Font( Tahoma F System Drawing FontStyle Regular System Drawing GraphicsUnit Point ((System Byte)( )));this dateTimePicker Cursor = System Windows Forms Cursors Arrow;this dateTimePicker Font = new System Drawing Font( Tahoma F System Drawing FontStyle Regular System Drawing GraphicsUnit Point ((System Byte)( )));this dateTimePicker Format = System Windows Forms DateTimePickerFormat Time;this dateTimePicker Location = new System Drawing Point( );this dateTimePicker Name = dateTimePicker ;this dateTimePicker RightToLeft = System Windows Forms RightToLeft No;this dateTimePicker Size = new System Drawing Size( );this dateTimePicker TabIndex = ;this dateTimePicker Value = new System DateTime( );this dateTimePicker ValueChanged += new System EventHandler(this dateTimePicker_ValueChanged);// // labelDateTimeNow// this labelDateTimeNow Font = new System Drawing Font( Tahoma F System Drawing FontStyle Regular System Drawing GraphicsUnit Point ((System Byte)( )));this labelDateTimeNow Location = new System Drawing Point( );this labelDateTimeNow Name = labelDateTimeNow ;this labelDateTimeNow Size = new System Drawing Size( );this labelDateTimeNow TabIndex = ;this labelDateTimeNow Text = 当前时间: ;this labelDateTimeNow TextAlign = System Drawing ContentAlignment MiddleLeft;// // labelDateTimeShutdown// this labelDateTimeShutdown Font = new System Drawing Font( Tahoma F System Drawing FontStyle Regular System Drawing GraphicsUnit Point ((System Byte)( )));this labelDateTimeShutdown Location = new System Drawing Point( );this labelDateTimeShutdown Name = labelDateTimeShutdown ;this labelDateTimeShutdown Size = new System Drawing Size( );this labelDateTimeShutdown TabIndex = ;this labelDateTimeShutdown Text = 任务时间: ;this labelDateTimeShutdown TextAlign = System Drawing ContentAlignment MiddleLeft;// // labelNow// this labelNow Font = new System Drawing Font( Tahoma F System Drawing FontStyle Bold System Drawing GraphicsUnit Point ((System Byte)( )));this labelNow ForeColor = System Drawing SystemColors ControlText;this labelNow Location = new System Drawing Point( );this labelNow Name = labelNow ;this labelNow Size = new System Drawing Size( );this labelNow TabIndex = ;this labelNow TextAlign = System Drawing ContentAlignment MiddleRight;// // labelShutdown// this labelShutdown Font = new System Drawing Font( Tahoma F System Drawing FontStyle Bold System Drawing GraphicsUnit Point ((System Byte)( )));this labelShutdown ForeColor = System Drawing SystemColors ControlText;this labelShutdown Location = new System Drawing Point( );this labelShutdown Name = labelShutdown ;this labelShutdown Size = new System Drawing Size( );this labelShutdown TabIndex = ;this labelShutdown TextAlign = System Drawing ContentAlignment MiddleRight;// // panelParent// this panelParent BorderStyle = System Windows Forms BorderStyle FixedSingle;this panelParent Controls Add(this panelChild);this panelParent Location = new System Drawing Point( );this panelParent Name = panelParent ;this panelParent Size = new System Drawing Size( );this panelParent TabIndex = ;// // panelChild// this panelChild Controls Add(this labelNow);this panelChild Controls Add(this labelShutdown);this panelChild Controls Add(this labelDateTimeShutdown);this panelChild Controls Add(this labelDateTimeNow);this panelChild Location = new System Drawing Point( );this panelChild Name = panelChild ;this panelChild Size = new System Drawing Size( );this panelChild TabIndex = ;// // buttonCancel// this buttonCancel Cursor = System Windows Forms Cursors Hand;this buttonCancel FlatStyle = System Windows Forms FlatStyle Flat;this buttonCancel Font = new System Drawing Font( Tahoma F System Drawing FontStyle Regular System Drawing GraphicsUnit Point ((System Byte)( )));this buttonCancel Location = new System Drawing Point( );this buttonCancel Name = buttonCancel ;this buttonCancel Size = new System Drawing Size( );this buttonCancel TabIndex = ;this buttonCancel Text = 取消&C ;this buttonCancel Click += new System EventHandler(this buttonCancel_Click);// // buttonInvoke// this buttonInvoke Cursor = System Windows Forms Cursors Hand;this buttonInvoke FlatStyle = System Windows Forms FlatStyle Flat;this buttonInvoke Font = new System Drawing Font( Tahoma F System Drawing FontStyle Regular System Drawing GraphicsUnit Point ((System Byte)( )));this buttonInvoke Location = new System Drawing Point( );this buttonInvoke Name = buttonInvoke ;this buttonInvoke Size = new System Drawing Size( );this buttonInvoke TabIndex = ;this buttonInvoke Text = 确定&O ;this buttonInvoke Click += new System EventHandler(this buttonInvoke_Click);// // checkBox// this checkBox Cursor = System Windows Forms Cursors Hand;this checkBox FlatStyle = System Windows Forms FlatStyle Flat;this checkBox Location = new System Drawing Point( );this checkBox Name = checkBox ;this checkBox Size = new System Drawing Size( );this checkBox TabIndex = ;this checkBox Text = 强制&B ;// // TimerComputerShutdown// this AutoScaleBaseSize = new System Drawing Size( );this BackColor = System Drawing Color GhostWhite;this ClientSize = new System Drawing Size( );this Controls Add(this checkBox);this Controls Add(this boBox);this Controls Add(this labelWhatDoYouWantTheComputerToDo);this Controls Add(this panelParent);this Controls Add(this buttonCancel);this Controls Add(this buttonInvoke);this Controls Add(this dateTimePicker);this Controls Add(this labelSelectDateTimeToShutdownComputer);this Font = new System Drawing Font( Tahoma F System Drawing FontStyle Regular System Drawing GraphicsUnit Point ((System Byte)( )));this FormBorderStyle = System Windows Forms FormBorderStyle FixedDialog;this Icon = ((System Drawing Icon)(resources GetObject( $this Icon )));this MaximizeBox = false;this Name = TimerComputerShutdown ;this StartPosition = System Windows Forms FormStartPosition CenterScreen;this Text = Timer Computer Shutdown ;this TopMost = true;this Load += new System EventHandler(this TimerComputerShutdown_Load);this panelParent ResumeLayout(false);this panelChild ResumeLayout(false);this ResumeLayout(false);

#endregion

/// <summary>/// The main entry point for the application /// </summary>[STAThread]static void Main() Application Run(new TimerComputerShutdown());

private void TimerComputerShutdown_Load(object sender System EventArgs e)this threadStart = new System Threading ThreadStart(Application_Tick);this thread = new System Threading Thread(threadStart);this panelChild Visible = false;

this dateTimePicker Value = System DateTime Now;

protected override void OnClosing(CancelEventArgs e)tryif (this thread IsAlive)this thread Abort();this thread Join();catch (System Threading ThreadAbortException threadAbortException)System Threading Thread ResetAbort();catch (System Exception exception)finallySystem Windows Forms Application Exit();

base OnClosing(e);

public void Computer_Shutdown()if (this checkBox Checked)System Diagnostics Process[] processes = System Diagnostics Process GetProcesses();

foreach (System Diagnostics Process processParent in processes)System Diagnostics Process[] processNames = System Diagnostics Process GetProcessesByName(processParent ProcessName);

foreach (System Diagnostics Process processChild in processNames)trySystem IntPtr hWnd = processChild MainWindowHandle;if (IsIconic(hWnd))ShowWindowAsync(hWnd SW_RESTORE);SetForegroundWindow(hWnd);if (!(processChild MainWindowTitle Equals(this Text)))processChild CloseMainWindow();processChild Kill();processChild WaitForExit();catch (System Exception exception)

System Windows Forms Application Exit();

switch (this boBox SelectedIndex)case :WindowsController ExitWindows(RestartOptions PowerOff false);break;case :WindowsController ExitWindows(RestartOptions Reboot false);break;case :WindowsController ExitWindows(RestartOptions Suspend false);break;case :WindowsController ExitWindows(RestartOptions Hibernate false);break;case :WindowsController ExitWindows(RestartOptions LogOff false);break;private void boBox_SelectedIndexChanged(object sender System EventArgs e)this labelSelectDateTimeToShutdownComputer Text = 选择任务 + + this boBox SelectedItem ToString() + + 时间: ;this labelDateTimeShutdown Text = 任务时间 + + this boBox SelectedItem ToString() + : ;

public bool dateTimePicker_Validated()if (this dateTimePicker Value CompareTo(System DateTime Now) < )Error error = new Error();error ShowDialog();

this dateTimePicker Value = System DateTime Now;

return false;elsereturn true;

private void dateTimePicker_ValueChanged(object sender System EventArgs e)this labelShutdown Text = this dateTimePicker Value ToString();

public void Application_Tick()while (!(this labelNow Text ToString() Equals(this dateTimePicker Value ToString())))this labelNow Text = System DateTime Now ToString();

this Computer_Shutdown();

private void buttonCancel_Click(object sender System EventArgs e)tryif (this thread IsAlive)this thread Abort();this thread Join();catch (System Threading ThreadStateException threadStateException)System Threading Thread ResetAbort();catch (System Exception exception)finallySystem Windows Forms Application Exit();private void buttonInvoke_Click(object sender System EventArgs e)if (this dateTimePicker_Validated())this thread Start();

this boBox Enabled = false;this dateTimePicker Enabled = false;this panelChild Visible = true;this buttonInvoke Enabled = false;

using System;using System Text;using System Diagnostics;using System Runtime InteropServices;

cha138/Article/program/net/201311/15683

相关参考

知识大全 用VB.NET 2005编写定时关机程序[3]

用VB.NET2005编写定时关机程序[3]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  双击

知识大全 用VB.NET 2005编写定时关机程序[2]

用VB.NET2005编写定时关机程序[2]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  双击

知识大全 用VB.NET 2005编写定时关机程序[1]

用VB.NET2005编写定时关机程序[1]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!&nbs

知识大全 用VB.NET 2005编写定时关机程序[4]

用VB.NET2005编写定时关机程序[4]  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!&nbs

知识大全 利用Delphi2005编写C#应用程序

利用Delphi2005编写C#应用程序  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!Delphi

知识大全 使用C#编写DES加密程序的framework

使用C#编写DES加密程序的framework  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  还

知识大全 Visual C#中编写多线程程序之起步

VisualC#中编写多线程程序之起步  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  net将关

知识大全 C#创建多线程应用程序

C#创建多线程应用程序  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  在NET和C#中编写一个多

知识大全 java定时启动线程

  在实际应用中很多情况需要定时运行程序比如分钟或者小时执行一次程序对此操作java和C#一样可以使用Timer计时器实现即javautilTimer中的Timer类  Timer的定义Timerti

知识大全 谁能告诉我怎么能编写电脑程序,我是电脑初学者。非常感谢

谁能告诉我怎么能编写电脑程序,我是电脑初学者。非常感谢!电脑程序有很多种啊大哥~~Java、Javascript、C#、C++、C、Actionscript……大多了。每种编程语言都有各自的特色。你根