c#实现一个自动关机的小工具
这两天突然需要用到自动关机的功能。从网上下了一个工具,没想到用两次居然要注册付费使用,晕倒,这点小功能也要付费啊。索性自己做一个吧!于是今天用C#自己做了一个,在这里晒一下!^_^!
自动关机功能很简单,你可以用API实现也可以用Command实现,就像我上篇文章提到的用shutdown.exe来实现,不过缺点是只有XP以上版本的系统才有(我想用2000和98的人已经很少了吧)。于是我就用Command实现吧!
先完成一个关机功能的类,这里的一点点技巧就是在C#中运行Command Line程序:
为了Cool一点,再加上一个最小化到图标栏的功能,并取消Close的功能。
最后再给这个程序加上一个Xml配置文件,就完成了^_^!
源代码在这里,注意我用的是VS2008开发的,所以呵呵!
自动关机功能很简单,你可以用API实现也可以用Command实现,就像我上篇文章提到的用shutdown.exe来实现,不过缺点是只有XP以上版本的系统才有(我想用2000和98的人已经很少了吧)。于是我就用Command实现吧!
先完成一个关机功能的类,这里的一点点技巧就是在C#中运行Command Line程序:
 1
        public static void Shutdown(bool isCancel, uint interval)
2
        {
3
            Process proc = new Process();
4
            proc.StartInfo.FileName = "cmd.exe"; // 启动命令行程序
5
            proc.StartInfo.UseShellExecute = false; // 不使用Shell来执行,用程序来执行
6
            proc.StartInfo.RedirectStandardError = true; // 重定向标准输入输出
7
            proc.StartInfo.RedirectStandardInput = true;
8
            proc.StartInfo.RedirectStandardOutput = true;
9
            proc.StartInfo.CreateNoWindow = true; // 执行时不创建新窗口
10
            proc.Start();
11
12
            string commandLine;
13
            if (isCancel)
14
                commandLine = @"shutdown /a";
15
            else
16
                commandLine = @"shutdown /f /s /t " + interval.ToString();
17
18
            proc.StandardInput.WriteLine(commandLine);
19
        }
然后再实现一个延时的功能,这个代码没什么好贴的就是运用DateTime和TimeSpan搞定。
        public static void Shutdown(bool isCancel, uint interval)2
        {3
            Process proc = new Process();4
            proc.StartInfo.FileName = "cmd.exe"; // 启动命令行程序5
            proc.StartInfo.UseShellExecute = false; // 不使用Shell来执行,用程序来执行6
            proc.StartInfo.RedirectStandardError = true; // 重定向标准输入输出7
            proc.StartInfo.RedirectStandardInput = true;8
            proc.StartInfo.RedirectStandardOutput = true;9
            proc.StartInfo.CreateNoWindow = true; // 执行时不创建新窗口10
            proc.Start();11

12
            string commandLine;13
            if (isCancel)14
                commandLine = @"shutdown /a";15
            else16
                commandLine = @"shutdown /f /s /t " + interval.ToString();17

18
            proc.StandardInput.WriteLine(commandLine);19
        }为了Cool一点,再加上一个最小化到图标栏的功能,并取消Close的功能。
 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
                notifyIcon.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.notifyIcon.Visible = false;
17
        }
18
在Close的事件处理函数中要注意, if (e.CloseReason != CloseReason.ApplicationExitCall) 因为我们取消掉了Close的功能,那么退出程序就用Application.Exit()来实现。
        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
                notifyIcon.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.notifyIcon.Visible = false;17
        }18

最后再给这个程序加上一个Xml配置文件,就完成了^_^!
 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></Hours>"
8
                            + "<Minutes></Minutes>"
9
                            + "</IntervalTime>"
10
                            + "</Config>");
11
            }
12
            else
13
            {
14
                doc.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();
            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></Hours>"8
                            + "<Minutes></Minutes>"9
                            + "</IntervalTime>"10
                            + "</Config>");11
            }12
            else13
            {14
                doc.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();源代码在这里,注意我用的是VS2008开发的,所以呵呵!
    将想法付诸于实践,借此来影响他人是一个人存在的真正价值
                    
                
        
            
                
            
        
浙公网安备 33010602011771号