private static bool InstallFont(string fontFile, string fontFileName, string fontName) { int Ret; //const int WM_FONTCHANGE = 0x001D; //const int HWND_BROADCAST = 0xffff; // -------------------------------------------- try { string destFile = OS.getFontsPath() + "\\" + fontFileName; if (!File.Exists(destFile)) { File.Copy(fontFile, destFile, true); } else { // -- GS.ShowMessage(destFile + " 已存在。"); -- } Ret = AddFontResource(destFile); // Res = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0); Ret = WriteProfileString("fonts", fontName + "(TrueType)", fontFile); } catch (Exception e) { GS.ShowError("添加字体文件 " + fontFile + " 遇到意外错误, 请检查.\n" + e.Message); return(false); } return(true); }
private static bool AddTrustSite_Domain(string strDomain) { #region -- 过程变量定义 -- RegistryKey regDomains = null, regDomain = null; #endregion try { regDomains = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains", true); if (regDomains == null) { regDomains = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains"); } string[] arrSubKeyNames = regDomains.GetSubKeyNames(); for (int j = 0; j < arrSubKeyNames.Length; j++) { regDomain = regDomains.OpenSubKey(arrSubKeyNames[j]); string[] arrValueNames = regDomain.GetValueNames(); for (int k = 0; k < arrValueNames.Length; k++) { if (arrValueNames[k].Equals(":Range")) { if (regDomain.GetValue(":Range").Equals(strDomain)) { return(true); } } } } // ---------------------------- string[] arrDomain = strDomain.Split('.'); if (arrDomain.Length == 1) { if (strDomain.Equals("localhost", StringComparison.CurrentCultureIgnoreCase)) { regDomain = regDomains.CreateSubKey(strDomain); regDomain.SetValue("http", 2, RegistryValueKind.DWord); } else { GS.ShowError("域名" + strDomain + "格式错误,无法加入到信任站点。"); return(false); } } else if (arrDomain.Length == 2) { regDomain = regDomains.CreateSubKey(strDomain); regDomain.SetValue("*", 2, RegistryValueKind.DWord); } else { strDomain = strDomain.Substring(arrDomain[0].Length + 1); regDomain = regDomains.CreateSubKey(strDomain); regDomain.SetValue("*", 2, RegistryValueKind.DWord); } } catch (Exception e) { GS.ShowError("添加信任站点域名 [" + strDomain + "] 失败,请重试。\r\n" + e.Message); return(false); } finally { } return(true); }
private static bool DownloadFile(string urlFile, string saveFile) { try { if (!OS.CheckFilePath(saveFile, true)) { return(false); } HttpWebRequest Myrq = (HttpWebRequest)System.Net.HttpWebRequest.Create(urlFile); HttpWebResponse myrp = (HttpWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; Stream st = myrp.GetResponseStream(); Stream so = new FileStream(saveFile, FileMode.Create); long totalDownloadedByte = 0; byte[] by = new byte[1024]; int osize = st.Read(by, 0, (int)by.Length); while (osize > 0) { totalDownloadedByte = osize + totalDownloadedByte; Application.DoEvents(); so.Write(by, 0, osize); osize = st.Read(by, 0, (int)by.Length); } so.Close(); st.Close(); } catch (Exception e) { GS.ShowError(e.Message); return(false); } return(true); }
private static bool DoResUpgrage(IniFile.IniResItem iri, string resFile) { bool blNoError = true; // -------------------------------------------- if (iri.method.Equals("")) { return(true); } // -------------------------------------------- if (iri.method.Equals("setup", StringComparison.CurrentCultureIgnoreCase)) { if (!UpgradeNewVersion(resFile)) { blNoError = false; } } else if (iri.method.Equals("trust_ip", StringComparison.CurrentCultureIgnoreCase)) { if (!AddTrustSite_IP(iri.value)) { blNoError = false; } } else if (iri.method.Equals("trust_domain", StringComparison.CurrentCultureIgnoreCase)) { if (!AddTrustSite_Domain(iri.value)) { blNoError = false; } } else if (iri.method.Equals("bat", StringComparison.CurrentCultureIgnoreCase)) { if (!ExecBAT(resFile)) { blNoError = false; } } else if (iri.method.Equals("copy", StringComparison.CurrentCultureIgnoreCase)) { if (!CopyResFile(resFile, iri.value, iri.key)) { blNoError = false; } } else if (iri.method.Equals("font", StringComparison.CurrentCultureIgnoreCase)) { if (!InstallFont(resFile, iri.key, iri.value)) { blNoError = false; } } else { GS.ShowError("未知的升级类型:" + iri.method + ",请检查。"); return(false); } return(blNoError); }
private void readIniFile() { if (this.iniFormat == 0) { readIniFile0(); } else if (this.iniFormat == 1) { readIniFile1(); } else { GS.ShowError("不支持的ini格式类型。"); } }
private static bool UpgradeNewVersion(string setupFile) { try { Process p = System.Diagnostics.Process.Start(setupFile); while (!p.HasExited) { System.Threading.Thread.Sleep(500); } } catch (Exception e) { GS.ShowError("升级客户端版本失败,请重试。\r\n" + e.Message); return(false); } finally { } return(true); }
private static bool ExecBAT(string batFile) { try { if (File.Exists(batFile)) { Process proc = new Process(); // proc.StartInfo.WorkingDirectory = "upgrade"; proc.StartInfo.FileName = batFile; proc.Start(); proc.WaitForExit(); } } catch (Exception e) { GS.ShowError("执行 " + batFile + " 遇到意外错误, 请检查.\n" + e.Message); return(false); } return(true); }
private static bool AddTrustSite_IP(string strIP) { #region -- 过程变量定义 -- RegistryKey regRanges = null, regRange = null; #endregion try { regRanges = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Ranges", true); if (regRanges == null) { regRanges = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Ranges"); } string[] arrSubKeyNames = regRanges.GetSubKeyNames(); for (int j = 0; j < arrSubKeyNames.Length; j++) { regRange = regRanges.OpenSubKey(arrSubKeyNames[j]); string[] arrValueNames = regRange.GetValueNames(); for (int k = 0; k < arrValueNames.Length; k++) { if (arrValueNames[k].Equals(":Range")) { if (regRange.GetValue(":Range").Equals(strIP)) { return(true); } } } } // ---------------------------------------- for (int j = 1; j <= 100; j++) { regRange = regRanges.OpenSubKey("Range" + j); if (regRange == null) { regRange = regRanges.CreateSubKey("Range" + j); regRange.SetValue(":Range", strIP, RegistryValueKind.String); regRange.SetValue("http", 2, RegistryValueKind.DWord); return(true); } } } catch (Exception e) { GS.ShowError("添加信任站点IP [" + strIP + "] 失败,请重试。\r\n" + e.Message); return(false); } finally { } return(true); }
private void btnOK_Click(object sender, EventArgs e) { int nIdx = 0; string strText = "", urlLogin = ""; byte[] data = null; FileStream fs = null; try { urlLogin = this.txtUrlLogin.Text.Trim(); if (!parseUrlLogin(urlLogin)) { return; } nIdx = urlLogin.IndexOf("rnd="); if (nIdx > 0) { urlLogin = urlLogin.Substring(0, nIdx - 1); } strText = "key = url_login; value = " + urlLogin; data = System.Text.Encoding.Default.GetBytes(strText); // -- 创建local.ini文件并写入初始内容 -- if (!Directory.Exists("upgrade")) { Directory.CreateDirectory("upgrade"); } fs = new FileStream("upgrade/local.ini", FileMode.Create); fs.Write(data, 0, data.Length); fs.Flush(); fs.Close(); fs = null; } catch (Exception e1) { GS.ShowError(e1.ToString()); } finally { if (fs != null) { fs.Close(); } } this.Close(); }
private static bool CopyResFile(string resFile, string destPath, string fileName) { string destFile = ""; // -------------------------------------------- try { destFile = destPath + "\\" + fileName; if (!Directory.Exists(destPath)) { Directory.CreateDirectory(destPath); } File.Copy(resFile, destFile, true); } catch (Exception e) { GS.ShowError("拷贝资源 " + resFile + " 遇到意外错误, 请检查.\n" + e.Message); return(false); } return(true); }
private static bool parseUrlLogin(string urlLogin) { #region -- 过程变量定义 -- int nIdx = 0; string strDomainIp = ""; #endregion #region -- 解析IP地址或域名部分 -- if (urlLogin.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase)) { strDomainIp = urlLogin.Substring(7); } else if (urlLogin.StartsWith("https://", StringComparison.CurrentCultureIgnoreCase)) { strDomainIp = urlLogin.Substring(8); } else { GS.ShowError("客户端配置文件 local.ini 中的 url_login 项格式不正确,请检查。\n" + urlLogin + "\n登录地址必需以 http:// 或 https:// 开头。"); return(false); } nIdx = strDomainIp.IndexOf("/"); if (nIdx <= 0) { GS.ShowError("客户端配置文件 local.ini 中的 url_login 项格式不正确,请检查。\n" + urlLogin); return(false); } strDomainIp = strDomainIp.Substring(0, nIdx); nIdx = strDomainIp.IndexOf(":"); if (nIdx > 0) { strDomainIp = strDomainIp.Substring(0, nIdx); } #endregion return(true); }
private void readIniFile1() { #region -- 过程变量定义 -- int nIdx = 0, lineNo = 0; string strLine = ""; string strKeyValue = "", strKey = "", strValue = ""; string[] arrResItem = null; StreamReader sr = null; #endregion try { if (!File.Exists(iniFileName)) { throw new Exception("配置文件 " + iniFileName + " 不存在,请检查。"); } sr = File.OpenText(iniFileName); while (sr.Peek() >= 0) { lineNo++; strLine = sr.ReadLine().Trim(); if (strLine.Length < 4) { continue; } else if (strLine.Substring(0, 4).Equals("rem ", StringComparison.CurrentCultureIgnoreCase)) { continue; } else { IniResItem iri = new IniResItem(); arrResItem = strLine.Split(';'); for (int i = 0; i < arrResItem.Length; i++) { strKeyValue = arrResItem[i]; if (strKeyValue.Trim() == "") { continue; } nIdx = strKeyValue.IndexOf("="); if (nIdx <= 0) { throw new Exception("\n配置文件 " + iniFileName + " 第 " + lineNo + " 行格式不正确,请检查:\n\n" + strLine + "\n"); } strKey = strKeyValue.Substring(0, nIdx).Trim(); strValue = strKeyValue.Substring(nIdx + 1).Trim(); if (strKey.Equals("key", StringComparison.CurrentCultureIgnoreCase)) { iri.key = strValue; } else if (strKey.Equals("type", StringComparison.CurrentCultureIgnoreCase)) { iri.type = strValue; } else if (strKey.Equals("method", StringComparison.CurrentCultureIgnoreCase)) { iri.method = strValue; } else if (strKey.Equals("value", StringComparison.CurrentCultureIgnoreCase)) { iri.value = strValue; } else if (strKey.Equals("version", StringComparison.CurrentCultureIgnoreCase)) { iri.version = strValue; } else if (strKey.Equals("path", StringComparison.CurrentCultureIgnoreCase)) { iri.path = strValue; } else { GS.ShowError("未知的 ini 配置项名称:" + strKey + ",已忽略。"); } } lstIri.Add(iri); } } } catch (Exception e) { throw e; } finally { if (sr != null) { sr.Close(); } } }
static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); FormWaitting frmWait = new FormWaitting(); frmWait.Show(); Application.DoEvents(); // --------------------------------------------- try { if (args.Length == 0) { GS.ShowError("调用参数不正确。"); return; } else if (args[0].Equals("debug")) { ignoreAutoUpgrade = true; } else if (args[0].Equals("_copy")) { invokerEXE = args[1]; File.Copy(GS.APP_EXE2, GS.APP_EXE, true); Thread.Sleep(100); Process.Start(GS.APP_EXE, "_self " + args[1]); return; } else if (args[0].Equals("_self")) { invokerEXE = args[1]; ignoreAutoUpgrade = true; } else { // -- invoked by abc.exe -- } if (!UpgradeResources()) { Console.WriteLine("升级失败。"); } // -------------------------------------------- if (flag.Equals("_copy")) // 本次更新了 AutoUpgrade.exe 自身 -- { Process.Start(GS.APP_EXE1, "_copy " + args[0]); } else if (args[0].Equals("_self")) { try { if (File.Exists(GS.APP_EXE1)) { File.Delete(GS.APP_EXE1); } if (File.Exists(GS.APP_EXE2)) { File.Delete(GS.APP_EXE2); } } catch (Exception ex) { GS.ShowError(ex); } Process.Start(args[1], "start"); } else if (args[0].Equals("debug")) { // -- VS调试环境,正常退出 -- GS.ShowMessage("VS调试环境 AutoUpgrade 升级成功完成。"); } else { Process.Start(args[0], "start"); } } catch (Exception e) { MessageBox.Show(e.ToString()); } }
private static bool UpgradeResources() { #region -- 过程变量定义 -- string versionClient; string fileName, fileSave = ""; IniFile.IniResItem iriServer; #endregion try { #region -- 1、读取本地local.ini,获取服务端资源路径 -- iniLocal = "upgrade/local.ini"; if (!File.Exists(iniLocal)) { GS.ShowError("配置文件 " + iniLocal + " 不存在,请检查。"); return(false); } iniFileClient = new IniFile(iniLocal, 1); urlServerPath = iniFileClient.getValue("url_server_path"); if (urlServerPath.Equals("")) { // -- 不执行自动升级 -- // -- GS.ShowError("参数 url_server_path 不存在,请检查。"); return(false); } if (!urlServerPath.EndsWith("/")) { urlServerPath += "/"; } urlServerIni = urlServerPath + "server.ini"; #endregion #region -- 2、下载并打开server.ini文件 -- iniServer = "upgrade/server.ini"; if (!DownloadFile(urlServerIni, iniServer)) { GS.ShowError("下载服务端版本文件\n" + urlServerIni + "\n失败。"); return(false); } iniFileServer = new IniFile(iniServer, 1); #endregion #region -- 3、打开client.ini文件 -- iniClient = "upgrade/client.ini"; if (!File.Exists(iniClient)) { FileStream fsTemp = new FileStream(iniClient, FileMode.Create); fsTemp.Close(); fsTemp = null; } iniFileClient = new IniFile(iniClient, 1); #endregion #region -- 4、比对资源项版本 -- for (int i = 0; i < iniFileServer.Count; i++) { iriServer = iniFileServer.getIniResItem(i); versionClient = iniFileClient.getVersion(iriServer.key, ""); if (versionClient.Equals(iriServer.version) && !iriServer.version.Equals("9999")) { continue; } // -- 资源项版本不同 -- if (iriServer.type.Equals("file", StringComparison.CurrentCultureIgnoreCase)) { fileName = iriServer.key; if ((fileName.Equals(GS.APP_EXE, StringComparison.CurrentCultureIgnoreCase)) && ignoreAutoUpgrade) { continue; } if (fileName.Equals(invokerEXE)) { fileSave = fileName; } else if (fileName.Equals(GS.APP_EXE, StringComparison.CurrentCultureIgnoreCase)) { fileSave = GS.APP_EXE1; } else { fileSave = iriServer.path + "\\" + fileName; } // -- 下载资源文件 -- if (!DownloadFile(urlServerPath + fileName, fileSave)) { GS.ShowError("新版本文件 " + fileName + " 下载失败,请确保该文件程序不在运行状态。"); return(false); } // -- 特殊资源文件 -- if (fileName.Equals(GS.APP_EXE)) { File.Copy(GS.APP_EXE1, GS.APP_EXE2, true); flag = "_copy"; return(true); } else if (fileSave.Equals(invokerEXE)) { continue; } } // -- 处理升级资源 -- fileSave = Application.StartupPath + "\\" + fileSave; if (!DoResUpgrage(iriServer, fileSave)) { return(false); } } #endregion #region -- 5、更新客户端版本文件 -- File.Copy("upgrade/server.ini", "upgrade/client.ini", true); File.Delete("upgrade/server.ini"); #endregion } catch (Exception e) { GS.ShowError(e.ToString()); return(false); } finally { } return(true); }