主程序:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows.Forms; 6 using System.IO; 7 using System.Diagnostics; 8 using System.Text.RegularExpressions; 9 namespace Maintenance 10 { 11 public partial class frm_Main : Form 12 { 13 int app_count = 0; 14 public frm_Main() 15 { 16 InitializeComponent(); 17 } 18 19 #region 火狐设置 20 private void Firefox_Set_button_Click(object sender, EventArgs e) 21 { 22 //主页设置 23 string Set_Path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"MozillaFirefoxprofiles.ini"; 24 string Path2 = ""; 25 List<string> str = new List<string>(); 26 using (StreamReader reader = File.OpenText(Set_Path)) 27 { 28 string line = ""; 29 while ((line = reader.ReadLine()) != null) 30 { 31 str.Add(line); 32 } 33 } 34 string NewPath = ""; 35 if (str.Where(n => n.Equals("[Profile0]")).ToArray().Length > 0) 36 { 37 string temp=str.Where(n => n.Contains("Path")).ToArray()[0]; 38 Path2 = temp.Substring(5, temp.Length - 5); 39 NewPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"MozillaFirefox" + Path2 + @"prefs.js"; 40 } 41 if (str.Where(n => n.Equals("[Profile1]")).ToArray().Length > 0) 42 { 43 string temp = str.Where(n => n.Contains("Path")).ToArray()[0]; 44 Path2 = temp.Substring(5, temp.Length - 5); 45 NewPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"MozillaFirefox" + Path2 ; 46 } 47 48 StringBuilder builder = new StringBuilder(); 49 using (StreamReader reader = File.OpenText(NewPath)) 50 { 51 string line = ""; 52 bool flag = true; 53 while ((line = reader.ReadLine()) != null) 54 { 55 if (line.Contains(""browser.startup.homepage"")&&flag==true) 56 { 57 builder.Append("user_pref("browser.startup.homepage", ""+this.HomePage_textBox.Text.Trim()+""); "); 58 flag = false; 59 } 60 else 61 { 62 builder.Append(line+" "); 63 } 64 } 65 } 66 File.WriteAllText(NewPath, builder.ToString()); 67 68 if(File.Exists(@"C:Program Files (x86)Mozilla Firefoxfirefox.exe")) 69 { 70 Process pro = Process.Start(@"C:Program Files (x86)Mozilla Firefoxfirefox.exe"); 71 pro.StartInfo.WindowStyle = ProcessWindowStyle.Maximized; 72 pro.WaitForInputIdle(1000); 73 if (pro.Responding) 74 { 75 SendKeys.SendWait("{F11}"); 76 } 77 } 78 } 79 #endregion 80 81 #region 界面初始化 82 83 private void Init() 84 { 85 //设置主页 86 this.HomePage_textBox.Text = System.Configuration.ConfigurationSettings.AppSettings["HomePage"]; 87 } 88 private void frm_Main_Load(object sender, EventArgs e) 89 { 90 this.Init(); 91 this.app_timer.Start(); 92 } 93 #endregion 94 95 #region 退出 96 private void Exit_button_Click(object sender, EventArgs e) 97 { 98 Application.Exit(); 99 } 100 #endregion 101 102 private void PingServer_button_Click(object sender, EventArgs e) 103 { 104 Process p = new Process(); 105 p.StartInfo.FileName = "cmd.exe"; 106 p.StartInfo.UseShellExecute = false; 107 p.StartInfo.RedirectStandardInput = true; 108 p.StartInfo.RedirectStandardOutput = true; 109 p.StartInfo.RedirectStandardError = true; 110 p.StartInfo.CreateNoWindow = true; 111 112 p.Start(); 113 p.StandardInput.WriteLine("ping -n " + System.Configuration.ConfigurationSettings.AppSettings["PingCount"] + " " + System.Configuration.ConfigurationSettings.AppSettings["ServerIP"]); 114 p.StandardInput.WriteLine("exit"); 115 string strRst = p.StandardOutput.ReadToEnd(); 116 //计数 117 int count = 0; 118 MatchCollection m1 = Regex.Matches(strRst, @"(0% loss)"); 119 MatchCollection m2 = Regex.Matches(strRst, @"Destination host unreachable."); 120 MatchCollection m3 = Regex.Matches(strRst, @"Request timed out."); 121 MatchCollection m4 = Regex.Matches(strRst, @"Unknown host"); 122 count = m1.Count + m2.Count + m3.Count + m4.Count;; 123 124 File.WriteAllText(System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"log.txt", strRst, Encoding.UTF8); 125 if (count >= int.Parse(System.Configuration.ConfigurationSettings.AppSettings["AlarmCount"])) 126 { 127 Process.Start(System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"log.txt"); 128 MessageBox.Show("网络不稳定,请检查!", "提示"); 129 } 130 else 131 { 132 File.Delete(System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"log.txt"); 133 MessageBox.Show("网络稳定!", "提示"); 134 } 135 } 136 137 #region 检查程序关闭时间 138 private void app_timer_Tick(object sender, EventArgs e) 139 { 140 app_count++; 141 if (app_count == 60 * int.Parse(System.Configuration.ConfigurationSettings.AppSettings["AppTime"])) 142 { 143 Application.Exit(); 144 } 145 } 146 #endregion 147 148 #region 一键设置客户端 149 private void ClientSet_button_Click(object sender, EventArgs e) 150 { 151 Firefox_Set_button_Click(null, null); 152 PingServer_button_Click(null,null); 153 } 154 #endregion 155 } 156 }
配置文件:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <appSettings> 4 <add key="HomePage" value="http://www.cnblogs.com/"/> <!--服务器主页--> 5 <add key="ServerIP" value="192.168.1.1"/> <!--服务器IP--> 6 <add key="PingCount" value="100"/> <!--Ping服务器次数--> 7 <add key="AlarmCount" value="5"/> <!--报警次数--> 8 <add key="AppTime" value="10"/> <!--程序运行时间,以分钟为单位--> 9 </appSettings> 10 </configuration>