c#實現一個自動關機的小工具

來源:互聯網
上載者:User
這兩天突然需要用到自動關機的功能。從網上下了一個工具,沒想到用兩次居然要註冊付費使用,暈倒,這點小功能也要付費啊。索性自己做一個吧!於是今天用C#自己做了一個,在這裡曬一下!^_^!
自動關機功能很簡單,你可以用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搞定。
為了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()來實現。
最後再給這個程式加上一個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();

原始碼在這裡,注意我用的是VS2008開發的,所以呵呵!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.