using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using Microsoft.Web; using Microsoft.Web.Administration; using System.Runtime.InteropServices; using System.IO; namespace WindowsFormsApplication3 { public partial class Form1 : Form { int logCount = 0; ServerManager sm; string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sys.ini"); /// /// 为INI文件中指定的节点取得字符串 /// /// 欲在其中查找关键字的节点名称 /// 欲获取的项名 /// 指定的项没有找到时返回的默认值 /// 指定一个字串缓冲区,长度至少为nSize /// 指定装载到lpReturnedString缓冲区的最大字符数量 /// INI文件完整路径 /// 复制到lpReturnedString缓冲区的字节数量,其中不包括那些NULL中止字符 [DllImport("kernel32")] private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); /// /// 修改INI文件中内容 /// /// 欲在其中写入的节点名称 /// 欲设置的项名 /// 要写入的新字符串 /// INI文件完整路径 /// 非零表示成功,零表示失败 [DllImport("kernel32")] private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName); public Form1() { InitializeComponent(); } void RecoveryWebSite() { int sleepTime = Convert.ToInt32(this.txtTime.Text); sleepTime = sleepTime * 60 * 60; try { string pools = this.txtcxc.Text; string sites = this.txtwz.Text; if (!string.IsNullOrEmpty(pools)) { foreach (string item in pools.Split(',')) { try { var pool = sm.ApplicationPools[item]; if (pool != null) { if (pool.State == ObjectState.Stopped) { if (pool.Start() == ObjectState.Started) { addlog("成功启动应用程序池:" + item); } else { addlog("启动应用程序池" + item + "失败. " + sleepTime / 60 + "秒后重试启动"); } } else { addlog("启动应用程序池:" + item + "已启动,无需启动"); } } else { addlog("未找到" + item + "应用程序池"); } } catch (Exception ex) { addlog("启动应用程序池" + item + "失败. " + sleepTime / 60 + "秒后重试启动!错误信息:" + ex.Message); } } } else { addlog("请先填写需要启动的应用程序池列表(多个应用程序池按英文逗号分隔)"); } if (!string.IsNullOrEmpty(sites)) { foreach (string item in sites.Split(',')) { try { var site = sm.Sites[item]; if (site != null) { if (site.State == ObjectState.Stopped) { if (site.Start() == ObjectState.Started) { addlog("成功启动网站:" + item); } else { addlog("启动网站" + item + "失败. " + sleepTime / 60 + "秒后重试启动"); } } else { addlog("网站:" + item + "已启动,无需启动"); } } else { addlog("未找到" + item + "网站"); } } catch (Exception ex) { addlog("启动网站" + item + "失败. " + sleepTime / 60 + "秒后重试启动!错误信息:" + ex.Message); } } } else { addlog("请先填写需要启动的网站列表(多个网站按英文逗号分隔)"); } } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); } GC.Collect(); Thread.Sleep(sleepTime); } public void addlog(string log) { this.Invoke(new EventHandler(delegate { log = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + log; this.txtLog.Text += log + "\r\n"; this.txtLog.Select(this.txtLog.TextLength, 0); this.txtLog.ScrollToCaret(); logCount++; if (logCount > 1000) { logCount = 0; // 日志过多 导致同步数据过慢,超过100条日志则清除。 this.txtLog.Text = ""; } })); } private void Form1_Load(object sender, EventArgs e) { sm = new ServerManager(); if (!File.Exists(filePath)) { File.Create(filePath); } else { this.txtcxc.Text = Read("s1", "pool", "", filePath); this.txtwz.Text = Read("s2", "site", "", filePath); } } private void btnStart_Click(object sender, EventArgs e) { timer1.Interval = Convert.ToInt32(this.txtTime.Text) * 60 * 60; timer1.Start(); // 记录本地ini文件 Write("s1", "pool", this.txtcxc.Text, filePath); Write("s2", "site", this.txtwz.Text, filePath); if (string.IsNullOrEmpty(this.txtTime.Text)) { this.txtTime.Text = "5"; } new Thread(RecoveryWebSite).Start(); } private void timer1_Tick(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.txtTime.Text)) { this.txtTime.Text = "5"; } new Thread(RecoveryWebSite).Start(); } /// /// 读取INI文件值 /// /// 节点名 /// 键 /// 未取到值时返回的默认值 /// INI文件完整路径 /// 读取的值 public static string Read(string section, string key, string def, string filePath) { StringBuilder sb = new StringBuilder(1024); GetPrivateProfileString(section, key, def, sb, 1024, filePath); return sb.ToString(); } /// /// 写INI文件值 /// /// 欲在其中写入的节点名称 /// 欲设置的项名 /// 要写入的新字符串 /// INI文件完整路径 /// 非零表示成功,零表示失败 public static int Write(string section, string key, string value, string filePath) { return WritePrivateProfileString(section, key, value, filePath); } /// /// 删除节 /// /// 节点名 /// INI文件完整路径 /// 非零表示成功,零表示失败 public static int DeleteSection(string section, string filePath) { return Write(section, null, null, filePath); } /// /// 删除键的值 /// /// 节点名 /// 键名 /// INI文件完整路径 /// 非零表示成功,零表示失败 public static int DeleteKey(string section, string key, string filePath) { return Write(section, key, null, filePath); } } }