/// <summary> /// 文件操作主入口 /// </summary> public static void ProceedFileOps(int setting, mainFrame mainForm) { string config_stream = Convert.ToString(setting, 2).PadLeft(16, '0'); bool IP_flag = Convert.ToBoolean(config_stream[9] - 0x30); //根据第10位设置IP协议模式 int insmode = (config_stream[12] - 0x30) * 2 + (config_stream[13] - 0x30); //根据第13, 14位设置安装源 CheckDirs(); //检查依赖文件目录 if (insmode == 0) //判断是否为本地安装模式 { if (CheckFile(IP_flag ? 1 : 0)) //检查依赖文件是否存在 return; else { //依赖文件不存在时自动下载相关文件 insmode = 1; msg.SetDialog(1, Resources.msg_info_ins); msg.ShowDialog(); } } DownBox downprog = new DownBox(mainForm); mainForm.Enabled = false; //锁定主窗口 downprog.Show(); try { DownFile(insmode, IP_flag, downprog); downprog.Close(); mainForm.Enabled = true; //解锁主窗口 mainForm.BringToFront(); //前置主窗口 return; } catch { //异常操作 downprog.Close(); mainForm.Enabled = true; //解锁主窗口 mainForm.BringToFront(); //前置主窗口 throw; } }
/// <summary> /// 执行操作 /// </summary> /// <param name="setting">控制码</param> public static void ProceedHostsOps(int setting, mainFrame mainForm) { SetConfig(setting); msg.SetDialog(0, Resources.msg_succ_ops); if (CheckConfig(setting)) //验证配置码 { if (backup_flag) { BackUpHosts(); //文件备份 msg.ShowDialog(); return; } if (recover_flag) { RecoverHosts(); //恢复备份 msg.ShowDialog(); return; } try { FileOps.ProceedFileOps(setting, mainForm); } catch { return; } mainForm.Enabled = false; msg.SetDialog(0, Resources.msg_succ_ops); ReadHosts(SetSecondaryPath()); try { AssembleHead(); AssembleHosts(region_flags, mainForm); ApplyHosts(); } catch { //Error 错误的文件 msg.SetDialog(3, Resources.msg_err_unr); } mainForm.Enabled = true; } else { //Error 错误的配置 msg.SetDialog(2, Resources.msg_alert_cfg); } msg.ShowDialog(); //修正主面板只是灯色彩 foreach (int asmCount in regionOrder) { if (region_flags[asmCount] == 1) { mainForm.TurnOn(asmCount); } } }
/// <summary> /// 生成hosts文件缓存 /// </summary> /// <param name="AsmFlags">标志位</param> private static void AssembleHosts(int[] AsmFlags, mainFrame mainForm) { string filename = @"hosts.tmp"; string TargetPath = Environment.CurrentDirectory; //设置缓存文件 string TargetFile = Path.Combine(TargetPath, filename); //缓存文件流 FileStream outfile = new FileStream(TargetFile, FileMode.Append); StreamWriter fileWriter = new StreamWriter(outfile, Encoding.Default); fileWriter.BaseStream.Seek(0, SeekOrigin.End); foreach (int asmCount in regionOrder) { bool hasError = false; if (AsmFlags[asmCount] == 1) { string prefix = "# region "; int regionStart = hostsStream.IndexOf(prefix + sectionName[asmCount]); if (regionStart != -1) { int regionEnd = hostsStream.IndexOf("# endregion", regionStart); int regionCount = regionEnd - regionStart + 1; string[] region = hostsStream.GetRange(regionStart, regionCount).ToArray(); //输出asmCount对应块hosts列表 foreach (string entry in region) { fileWriter.WriteLine(entry); } fileWriter.Flush(); //修改主窗口图标 mainForm.TurnGreen(asmCount); } else { //Error 文件缺少内容 hasError = true; mainForm.TurnRed(asmCount); } } if (hasError) { //Error 文件缺少内容 msg.SetDialog(2, Resources.msg_err_res); msg.ShowDialog(); } } fileWriter.Close(); }