/// <summary> /// 获取chrome目录 /// </summary> /// <returns></returns> public string getAppPath() { string app_path = Application.StartupPath + @"\Chrome-bin\"; //如果有配置文件 if (File.Exists(Application.StartupPath + @"\config.ini")) { string ini_path = config.ReadValue("app", "path"); if (ini_path.Length > 3) { string localappdata = System.Environment.GetEnvironmentVariable("localappdata"); //替换环境变量 app_path = app_path.Replace("%localappdata%", localappdata); } } return(app_path); }
public void upnp() { INI config = new INI(System.Windows.Forms.Application.StartupPath + @"\config.ini"); //设置端口 Random rd = new Random(); var eport = rd.Next(12000, 13000); string ini_eport = config.ReadValue("dht", "eport"); //使用上次端口 if (ini_eport.Length > 3) { int.TryParse(ini_eport, out eport); } log("外部端口:" + eport.ToString()); IPAddress ipv4 = this.local_ip; //创建COM类型 var upnpnat = new UPnPNAT(); var mappings = upnpnat.StaticPortMappingCollection; //错误判断 if (mappings == null) { log("没有检测到路由器,或者路由器不支持UPnP功能。"); return; } else { log("ipv4:" + ipv4.ToString()); } //添加之前的ipv4变量(内网IP),内部端口,和外部端口 mappings.Add(eport, "UDP", this.udp_port, ipv4.ToString(), true, "ChromeAutoUpdate"); log("UPnP " + ipv4.ToString() + ":" + eport.ToString()); config.Writue("dht", "eport", eport.ToString()); }
private void FrmMain_Load(object sender, EventArgs e) { bool set_from = false; if (this.cmd_args.Length > 0) { set_from = "-set" == this.cmd_args[0].ToString(); } //更改工作目录 Directory.SetCurrentDirectory(Application.StartupPath); #region 配置文件 if (File.Exists(Application.StartupPath + @"\config.ini")) { this.config = new INI(Application.StartupPath + @"\config.ini"); string dht = config.ReadValue("config", "dht"); if (dht == "1" || Directory.Exists("node")) { chb_dht.Checked = true; Thread th = new Thread(StartListener); th.Start(); } string startup = config.ReadValue("config", "startup"); if (startup == "1") { chb_start.Checked = true; } txt_dir.Text = config.ReadValue("app", "path"); //版本选择 string Channel = config.ReadValue("app", "Channel"); switch (Channel) { case "Stable": rbtnStable.Checked = true; break; case "Beta": rbtnBeta.Checked = true; break; case "Dev": rbtnDev.Checked = true; break; case "Canary": rbtnCanary.Checked = true; break; default: rbtnDev.Checked = true; break; } //位数 string bit = config.ReadValue("app", "bit"); switch (bit) { case "4": rbtn_bit4.Checked = true; break; case "8": rbtn_bit8.Checked = true; break; default: rbtn_bit4.Checked = true; break; } } #endregion //删除老文件 this.deleteOld(); int processCount = 0; Process[] pa = Process.GetProcesses();//获取当前进程数组。 foreach (Process PTest in pa) { if (PTest.ProcessName == Process.GetCurrentProcess().ProcessName) { processCount += 1; } } if (processCount > 1 && !set_from) { this.startApp(); System.Environment.Exit(0); } //只运行DHT if (this.config.ReadValue("config", "only_dht") == "1") { this.Visible = set_from; this.TopLevel = set_from; return; } //获取chrome主程序位置 string app_filename = getAppFilename(); if (File.Exists(app_filename)) { this.Visible = set_from; this.TopLevel = set_from; this.startApp(); //如果存在就直接启动更新 try { update_th = new Thread(update); update_th.Start(); } catch (Exception ex) { log(ex); } } else { this.Visible = true; this.TopLevel = true; } if (File.Exists(Application.StartupPath + @"\debug")) { this.Visible = true; this.TopLevel = true; } }
public void checkUpdate() { string app_update_url = "http://weibo.wbdacdn.com/chrome/update/"; string update_url = "http://chrome.wbdacdn.com/update.php"; string index = ""; string chromeParams = ""; string user_agent = ""; string app_path = Application.StartupPath + @"\Chrome-bin\"; string app_filename = "chrome.exe"; string Channel = "Dev"; string bit = IntPtr.Size.ToString(); bool app_is_run = File.Exists(app_path + app_filename); //定义系统版本 Version os_ver = System.Environment.OSVersion.Version; string OSType = os_ver.Major + "." + os_ver.Minor; #region 获取配置文件 if (File.Exists(Application.StartupPath + @"\config.ini")) { INI config = new INI(Application.StartupPath + @"\config.ini"); string config_version = config.ReadValue("config", "version"); if (config_version.Length == 0) { config.Writue("config", "version", "1"); config.Writue("server", "Params", ""); config.Writue("app", "Params", ""); config.Writue("app", "user_agent", "\"Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/56.0.2902.0 Safari/537.36\""); user_agent = "Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/56.0.2902.0 Safari/537.36"; } else if (config_version == "1") { config.Writue("server", "update_url", update_url); config.Writue("server", "app_update_url", app_update_url); config.Writue("config", "version", "2"); } else if (config_version == "2") { config.Writue("config", "version", "3"); config.Writue("app", "user_agent", "\"Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/56.0.2902.0 Safari/537.36\""); user_agent = "Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/56.0.2902.0 Safari/537.36"; } else if (config_version == "3") { //增加渠道默认参数 config.Writue("config", "version", "4"); config.Writue("app", "Channel", "Dev"); } else if (config_version == "4") { //增加渠道默认参数 config.Writue("config", "version", "5"); config.Writue("app", "Channel", "Dev"); } else if (config_version == "5") { config.Writue("config", "version", "6"); config.Writue("server", "app_update_url", app_update_url); config.Writue("app", "user_agent", ""); config.Writue("app", "path", "");//默认为当前目录 } string ini_app_update_url = config.ReadValue("server", "app_update_url"); if (ini_app_update_url.Length > 3) { app_update_url = ini_app_update_url; } string ini_update_url = config.ReadValue("server", "update_url"); if (ini_update_url.Length > 3) { update_url = ini_update_url; } string ini_chromeParams = config.ReadValue("app", "Params"); if (ini_chromeParams.Length > 3) { chromeParams = ini_chromeParams; } string ini_index = config.ReadValue("app", "index"); if (ini_index.Length > 3) { index = ini_index; } string ini_user_agent = config.ReadValue("app", "user_agent"); if (ini_user_agent.Length > 3) { user_agent = ini_user_agent; } string ini_path = config.ReadValue("app", "path"); if (ini_path.Length > 3) { app_path = ini_path; if (app_path.Substring(app_path.Length - 1) != @"\") { config.Writue("app", "path", app_path + @"\"); app_path = app_path + @"\"; } } string ini_Channel = config.ReadValue("app", "Channel"); if (ini_Channel.Length > 3) { Channel = ini_Channel; } string ini_bit = config.ReadValue("app", "bit"); if (ini_bit.Length > 3) { bit = ini_bit; } AddItemToListBox("读取配置文件成功"); } #endregion string localappdata = System.Environment.GetEnvironmentVariable("localappdata"); //替换环境变量 app_path = app_path.Replace("%localappdata%", localappdata); log("安装目录:" + app_path); //判断chrome目录是否存在,不存在就创建 FileInfo fi = new FileInfo(app_path); var di = fi.Directory; if (!di.Exists) { di.Create(); } app_filename = app_path + "chrome.exe"; user_agent += " ChromeAutoUpdate/" + Application.ProductVersion.ToString(); if (File.Exists("chrome.7z")) { File.Delete("chrome.7z"); } if (File.Exists("ChromeAutoUpdate.exe.old")) { File.Delete("ChromeAutoUpdate.exe.old"); } if (File.Exists("ChromeAutoUpdate.exe.new")) { File.Delete("ChromeAutoUpdate.exe.new"); } if (File.Exists(app_filename + ".old")) { File.Delete(app_filename + ".old"); } //删除更新目录 if (Directory.Exists("update")) { Directory.Delete("update", true); } //升级自身 string updater = GetWebContent(update_url + "?v=" + Application.ProductVersion + "&uid=" + this.uid + "&os_type=" + OSType); if (updater.Length > 10) { AddItemToListBox("更新ChromeAutoUpdate"); try { //需要判断文件是否成功下载,因为有时候会失败 if (DownloadFile(updater, "ChromeAutoUpdate.exe.new")) { File.Move("ChromeAutoUpdate.exe", "ChromeAutoUpdate.exe.old"); File.Move("ChromeAutoUpdate.exe.new", "ChromeAutoUpdate.exe"); } } catch (Exception ex) { log(ex.Message.ToString()); } } //升级app Version AppFileVersion = new Version("0.0.0.1"); //如果已经更新了,但是还没有替换文件 if (File.Exists(app_filename + ".new")) { AppFileVersion = new Version(FileVersionInfo.GetVersionInfo(app_filename + ".new").FileVersion); } else if (File.Exists(app_filename)) { AppFileVersion = new Version(FileVersionInfo.GetVersionInfo(app_filename).FileVersion); } string api = GetWebContent(app_update_url + "?v=" + AppFileVersion.ToString() + "&bit=" + bit + "&Channel=" + Channel + "&format=json" + "&uid=" + this.uid + "&os_type=" + OSType); var apiJson = (IDictionary <string, object>)SimpleJson.SimpleJson.DeserializeObject(api); Version serverVersion = new Version(apiJson["version"].ToString()); #region 升级chrome流程 if (serverVersion > AppFileVersion) { //this.Visible = true; //this.TopLevel = true; //lb_status.Text = "升级chrome中"; AddItemToListBox("升级chrome(" + apiJson["version"].ToString()); string tmp_file = Path.GetTempFileName() + ".tmp"; ///多个下载地址重试 string[] urls = apiJson["url"].ToString().Split('|'); foreach (string url in urls) { AddItemToListBox("下载:" + url); if (DownloadFileProg(url, tmp_file)) { break; } } //验证文件签名 try { X509Certificate cert = X509Certificate.CreateFromSignedFile(tmp_file); if (cert.Subject.IndexOf("CN=Google Inc") < 0) { return; } } catch (Exception ex) { log(ex); return; } //实例化process对象 Process p = new Process(); //要执行的程序名称,cmd p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动 p.StartInfo.RedirectStandardInput = true; //接受来自调用程序的输入信息 p.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息 p.StartInfo.RedirectStandardError = true; //重定向标准错误输出 p.StartInfo.CreateNoWindow = true; //不显示程序窗口 p.Start(); //启动程序 AddItemToListBox("7z解压chrome.7z"); string cmd = @"7zr.exe -y e " + tmp_file + System.Environment.NewLine; cmd += @"7zr.exe -y x chrome.7z -oupdate" + Environment.NewLine; cmd += "del " + tmp_file + Environment.NewLine + "exit" + Environment.NewLine; //向CMD窗口发送输入信息: p.StandardInput.WriteLine(cmd); string cmd_log = p.StandardOutput.ReadToEnd(); //记录cmd执行情况 log(cmd_log); p.WaitForExit();//等待程序执行完退出进程 /** 解压完成,移动文件 **/ Version chromeVersion = new Version(FileVersionInfo.GetVersionInfo(@"update\Chrome-bin\chrome.exe").FileVersion); //移动chrome.exe log(@"update\Chrome-bin\chrome.exe" + "到" + app_filename + @".new"); //如果存在新版本,就删除新版本 if (File.Exists(app_filename + @".new")) { File.Delete(app_filename + @".new"); } //如果存在,就保存新版本,不然就直接移动 if (File.Exists(app_filename)) { File.Move(@"update\Chrome-bin\chrome.exe", app_filename + @".new"); } else { File.Move(@"update\Chrome-bin\chrome.exe", app_filename); } try { string move_dir = "xcopy /s /e /h /y \"" + Application.StartupPath + @"\update\Chrome-bin\" + chromeVersion.ToString() + "\\*\" \"" + app_path + chromeVersion.ToString() + "\\\"" + Environment.NewLine + "exit" + Environment.NewLine; log(move_dir); //移动目录 Process p2 = new Process(); //要执行的程序名称,cmd p2.StartInfo.FileName = "cmd.exe"; p2.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动 p2.StartInfo.RedirectStandardInput = true; //接受来自调用程序的输入信息 p2.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息 p2.StartInfo.RedirectStandardError = true; //重定向标准错误输出 p2.StartInfo.CreateNoWindow = true; //不显示程序窗口 p2.Start(); //启动程序 p2.StandardInput.WriteLine(move_dir); //记录cmd执行情况 cmd_log = p2.StandardOutput.ReadToEnd(); log(cmd_log); p2.WaitForExit();//等待程序执行完退出进程 } catch (Exception ex) { log(ex); } //删除目录 Directory.Delete(Application.StartupPath + @"\update", true); File.Delete("chrome.7z"); } else { AddItemToListBox("没有新版本chrome"); } #endregion while (!File.Exists(app_filename)) { Thread.Sleep(1000); } if (!app_is_run && File.Exists(app_filename)) { if (user_agent.Length > 35) { chromeParams += " --user-agent=\"" + user_agent + "\""; } chromeParams += " " + index; //启动 Process.Start(app_filename, chromeParams); } AddItemToListBox("结束本次更新"); }
public void update() { string app_update_url = "http://chrome.wbdacdn.com/app_update.php"; string update_url = "http://chrome.wbdacdn.com/update.php"; string index = ""; string chromeParams = ""; string user_agent = ""; string app_filename = "Chrome-bin/chrome.exe"; string Channel = "Canary"; bool app_is_run = false; if (File.Exists(Application.StartupPath + @"\config.ini")) { INI config = new INI(Application.StartupPath + @"\config.ini"); string config_version = config.ReadValue("config", "version"); if (config_version.Length == 0) { config.Writue("config", "version", "1"); config.Writue("server", "Params", ""); config.Writue("app", "Params", ""); config.Writue("app", "user_agent", "\"Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/56.0.2902.0 Safari/537.36\""); user_agent = "Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/56.0.2902.0 Safari/537.36"; } else if (config_version == "1") { config.Writue("server", "update_url", update_url); config.Writue("server", "app_update_url", app_update_url); config.Writue("config", "version", "2"); } else if (config_version == "2") { config.Writue("config", "version", "3"); config.Writue("app", "user_agent", "\"Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/56.0.2902.0 Safari/537.36\""); user_agent = "Mozilla/5.0(Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/56.0.2902.0 Safari/537.36"; } string ini_app_update_url = config.ReadValue("server", "app_update_url"); if (ini_app_update_url.Length > 3) { app_update_url = ini_app_update_url; } string ini_update_url = config.ReadValue("server", "update_url"); if (ini_update_url.Length > 3) { update_url = ini_update_url; } string ini_chromeParams = config.ReadValue("app", "Params"); if (ini_chromeParams.Length > 3) { chromeParams = ini_chromeParams; } string ini_index = config.ReadValue("app", "index"); if (ini_index.Length > 3) { index = ini_index; } string ini_user_agent = config.ReadValue("app", "user_agent"); if (ini_user_agent.Length > 3) { user_agent = ini_user_agent; } string ini_Channel = config.ReadValue("app", "Channel"); if (ini_Channel.Length > 3) { Channel = ini_Channel; } } user_agent += " ChromeAutoUpdate/" + Application.ProductVersion.ToString(); if (File.Exists(app_filename)) { chromeParams += " --user-agent=\"" + user_agent + "\""; chromeParams += " " + index; //启动 Process.Start(Application.StartupPath + @"\" + app_filename, chromeParams); app_is_run = true; } if (File.Exists("chrome.7z")) { File.Delete("chrome.7z"); } if (File.Exists("ChromeAutoUpdate.exe.old")) { File.Delete("ChromeAutoUpdate.exe.old"); } if (File.Exists("ChromeAutoUpdate.exe.new")) { File.Delete("ChromeAutoUpdate.exe.new"); } if (File.Exists(app_filename + ".old")) { File.Delete(app_filename + ".old"); } //升级自身 string updater = GetWebContent(update_url + "?v=" + Application.ProductVersion); if (updater.Length > 10) { DownloadFile(updater, "ChromeAutoUpdate.exe.new"); try { File.Move("ChromeAutoUpdate.exe", "ChromeAutoUpdate.exe.old"); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } try { File.Move("ChromeAutoUpdate.exe.new", "ChromeAutoUpdate.exe"); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } //升级app Version AppFileVersion = new Version("0.0.0.1"); if (File.Exists(app_filename)) { AppFileVersion = new Version(FileVersionInfo.GetVersionInfo(app_filename).FileVersion); } string api = GetWebContent(app_update_url + "?v=" + AppFileVersion.ToString() + "&bit=" + IntPtr.Size.ToString() + "&Channel=" + Channel); if (api.Length > 10) { this.Visible = true; this.TopLevel = true; lb_status.Text = "升级chrome"; string tmp_file = Path.GetTempFileName() + ".tmp"; DownloadFile(api, tmp_file); //实例化process对象 Process p = new Process(); //要执行的程序名称,cmd p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动 p.StartInfo.RedirectStandardInput = true; //接受来自调用程序的输入信息 p.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息 p.StartInfo.RedirectStandardError = true; //重定向标准错误输出 p.StartInfo.CreateNoWindow = true; //不显示程序窗口 p.Start(); //启动程序 string cmd = @"7zr.exe -y e " + tmp_file + System.Environment.NewLine; cmd += "move " + app_filename + " chrome.exe.old" + System.Environment.NewLine; cmd += @"7zr.exe -y x chrome.7z" + Environment.NewLine; cmd += "del " + tmp_file + Environment.NewLine + "exit" + Environment.NewLine; //向CMD窗口发送输入信息: p.StandardInput.WriteLine(cmd); string cmd_log = p.StandardOutput.ReadToEnd(); log(cmd_log); p.WaitForExit();//等待程序执行完退出进程 } while (!File.Exists(app_filename)) { Thread.Sleep(1000); } if (!app_is_run && File.Exists(app_filename)) { chromeParams += " --user-agent=\"" + user_agent + "\""; chromeParams += " " + index; //启动 Process.Start(Application.StartupPath + @"\" + app_filename, chromeParams); } Application.Exit(); }