The automatic shutdown function is suddenly required in the past two days. I got a tool from the internet. I didn't expect to use it for registration and payment twice. I fainted. I also paid for this small feature. Simply do it yourself! So today I made one by myself with C #. Let's share it here! ^_^!
The automatic shutdown function is very simple. You can use the API or command to implement it, just as in my previous article Article Shutdown.exe is used for implementation, but the disadvantage is that only systems of XP and later versions are available (I think there are very few people using 2000 and 98 ). So I will use command to implement it!
First, complete a shutdown function class. The trick here is to run command line in C #. Program : 1 Public Static Void Shutdown ( Bool Iscancel, Uint Interval)
2 {
3 Process proc = New Process ();
4 Proc. startinfo. filename = " Cmd.exe " ; // Start the command line program
5 Proc. startinfo. useshellexecute = False ; // Do not use shell to execute, use a program to execute
6 Proc. startinfo. redirectstandarderror = True ; // Redirect standard input/output
7 Proc. startinfo. redirectstandardinput = True ;
8 Proc. startinfo. redirectstandardoutput = True ;
9 Proc. startinfo. createnowindow = True ; // No new window is created during execution
10 Proc. Start ();
11
12 String CommandLine;
13 If (Iscancel)
14 CommandLine = @" Shutdown/ " ;
15 Else
16 CommandLine = @" Shutdown/f/S/T " + Interval. tostring ();
17
18 Proc. standardinput. writeline (CommandLine );
19 }
And then implement a latency function.Code Nothing to do is to use datetime and timespan.
For cool, add a function that minimizes to the icon bar and cancel the close function. 1 Private Void Autoshutdownform_formclosing ( Object Sender, formclosingeventargs E)
2 {
3 If (E. closereason ! = Closereason. applicationexitcall)
4 {
5 E. Cancel = True ;
6 This . Windowstate = Formwindowstate. minimized;
7 This . Visible = False ;
8 Policyicon. Visible = True ;
9 }
10 }
11
12 Private Void Notifyicon_mousedoubleclick ( Object Sender, mouseeventargs E)
13 {
14 This . Visible = True ;
15 This . Windowstate = Formwindowstate. normal;
16 This . Policyicon. Visible = False ;
17 }
18
Note the following in the close event processing function, If (E. closereason ! = Closereason. applicationexitcall) Because we cancel the close function, we can use application. Exit () to exit the program.
Add an xml configuration file to the program, and then complete the configuration!1 Xmldocument Doc = New Xmldocument ();
2 If ( ! File. exists (s_config_file ))
3 {
4 Doc. loadxml ( " <? XML version = \ " 1.0 \ " Encoding = \ " UTF - 8 \ " ?> "
5 + " <Config> "
6 + " <Intervaltime> "
7 + " <Hours> "
8 + " <Minutes> </minutes> "
9 + " </Intervaltime> "
10 + " </Config> " );
11 }
12 Else
13 {
14Doc. Load (s_config_file );
15}
16
17 Xmlnode intervalnode = Doc. documentelement. selectsinglenode ( @" Intervaltime " );
18 Intervalnode. selectsinglenode ( @" Hours " ). Innertext = S_hours.tostring ();
19 Intervalnode. selectsinglenode ( @" Minutes " ). Innertext = S_minutes.tostring ();
20
21 Xmltextwriter xtw = New Xmltextwriter (s_config_file, Null );
22 Xtw. Formatting = Formatting. indented;
23 Xtw. indentation = 4 ;
24 Doc. writecontentto (xtw );
25 Xtw. Flush ();
26 Xtw. Close ();
The source code is here. Note that I use vs2008 for development!