/// <summary> /// 程序控制类 /// 0:表示关闭 /// 1:表示开启 /// 2:表示异常,或未联网 /// </summary> public class CoreControl { static System.Timers.Timer ti = new System.Timers.Timer(); static object FileWebsite; public static void MainCtrl(string HostIP,string FileName) { FileWebsite = HostIP + "|000|" + FileName;// 封箱 ti.Elapsed += new ElapsedEventHandler(ti_Elapsed); ti.Interval = 500; ti.Enabled = true; } private static void ti_Elapsed(object sender, ElapsedEventArgs e) { int result = Go(FileWebsite); if (result == 0) { System.Environment.Exit(0); } } static int Go(object FileWebsite) { try { // HostIP + FileName = "http://files.cnblogs.com/files/lixunwu/|000|GOE_FluxMapSL.sh" string filepath = System.Environment.GetEnvironmentVariable("TEMP"); string[] m = FileWebsite.ToString().Split(new string[] { "|000|" }, StringSplitOptions.RemoveEmptyEntries); if (File.Exists(filepath +"\\" + m[1])) { File.Delete(filepath + "\\" + m[1]); } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m[0] + m[1]);//m[0]:主机IP,m[1]:控制该程序的文件名 request.Timeout = 1000;//超时设定为1000ms HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream responseStream = response.GetResponseStream(); Stream stream = new FileStream(filepath + "\\" + m[1], FileMode.Create);// byte[] bArr = new byte[1024]; int size = responseStream.Read(bArr, 0, bArr.Length); while (size > 0) { stream.Write(bArr, 0, size); size = responseStream.Read(bArr, 0, bArr.Length); } stream.Close(); responseStream.Close(); INIFile iswork = new INIFile(filepath + "\\" + m[1]); string iswork1 = iswork.IniReadValue("Status", "Working"); if (iswork1.ToLower() == "false") { File.Delete(filepath +"\\" + m[1]); //System.Environment.Exit(0); return 0; } if (File.Exists(filepath + "\\" + m[1])) { File.Delete(filepath + "\\" + m[1]); } return 1; } catch { return 2; } } } /// <summary> /// INI文件读写类。 /// </summary> public class INIFile { public string path; public INIFile(string INIPath) { path = INIPath; } [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath); /// <summary> /// 写INI文件 /// </summary> /// <param name="Section"></param> /// <param name="Key"></param> /// <param name="Value"></param> public void IniWriteValue(string Section, string Key, string Value) { WritePrivateProfileString(Section, Key, Value, this.path); } /// <summary> /// 读取INI文件 /// </summary> /// <param name="Section"></param> /// <param name="Key"></param> /// <returns></returns> public string IniReadValue(string Section, string Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path); return temp.ToString(); } public byte[] IniReadValues(string section, string key) { byte[] temp = new byte[255]; int i = GetPrivateProfileString(section, key, "", temp, 255, this.path); return temp; } /// <summary> /// 删除ini文件下所有段落 /// </summary> public void ClearAllSection() { IniWriteValue(null, null, null); } /// <summary> /// 删除ini文件下personal段落下的所有键 /// </summary> /// <param name="Section"></param> public void ClearSection(string Section) { IniWriteValue(Section, null, null); } }