/// <summary> /// 保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_save_Click(object sender, EventArgs e) { CloseFormFlag = true; BLL.Mission m_BLL = new BLL.Mission(); string N = Convert.ToString(m_BLL.Select(txt_spidername.Text)[0]).Trim(); string S = Convert.ToString(m_BLL.Select(txt_spidername.Text)[1]).Trim(); if (S == "delete") { m_BLL.Delete(N); } if (N == "" || S == "delete" || spiderName != null)//进入条件:1,新建任务时,表中没有该任务;2,新建任务时,表中有该任务但是状态为废弃;3,修改任务; { #region 保存爬虫配置 //获取信息 SI_Mod.spidername = txt_spidername.Text; SI_Mod.rules = SI_Mod.spidername; SI_Mod._class = SI_Mod.spidername; SI_Mod.website = txt_website.Text; SI_Mod.start_urls = txt_starturls.Text; SI_Mod.allowed_domains = txt_domains.Text; SI_Mod.file_store = txt_filestore.Text; if (comb_loader.SelectedItem != null) { SI_Mod.loader = comb_loader.SelectedItem.ToString(); } if (comb_away.SelectedItem != null) { SI_Mod.away = comb_away.SelectedItem.ToString(); } SI_Mod.customdata = string.Join(":", (string[])ClsRule_List.ToArray(typeof(string))); SI_Mod.cls = string.Join(":", (string[])Cls_List.ToArray(typeof(string))); //在get模式下,post的配置将不存储 if (comb_away.SelectedItem != null && comb_away.SelectedItem.ToString() == "post") { //构造账户信息字典字符串 Dictionary <string, string> account = new Dictionary <string, string>(); account.Add(text_userlab.Text, txt_usr.Text); account.Add(text_pwdlab.Text, txt_pwd.Text); SI_Mod.account = JsonConvert.SerializeObject(account); //登录url SI_Mod.login_url = txt_loginurl.Text; //cookies if (Cookies_List.Count == 0) { SI_Mod.cookies = ""; } else { SI_Mod.cookies = string.Join("#", (string[])Cookies_List.ToArray(typeof(string))); } } //rulesdata SI_Mod.ruledatas = string.Join("#", (string[])Step_List.ToArray(typeof(string))); #endregion #region 保存数据库信息 //存入配置文件 //获取Configuration对象 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //写入<add>元素的Value config.AppSettings.Settings["Server"].Value = txt_SqlIp.Text; config.AppSettings.Settings["Port"].Value = txt_SqlPort.Text; config.AppSettings.Settings["Database"].Value = txt_DBName.Text; config.AppSettings.Settings["User"].Value = txt_SqlUsr.Text; config.AppSettings.Settings["Password"].Value = txt_SqlPwd.Text; config.AppSettings.Settings["SqlStr"].Value = MakeSqlStr(); if (comb_SqlType.SelectedItem != null) { config.AppSettings.Settings["SqlType"].Value = comb_SqlType.SelectedItem.ToString(); } //一定要记得保存,写不带参数的config.Save()也可以 config.Save(ConfigurationSaveMode.Modified); //刷新,否则程序读取的还是之前的值(可能已装入内存) ConfigurationManager.RefreshSection("appSettings"); //存入数据库 HI_Mod.spidername = txt_spidername.Text; HI_Mod.server = txt_SqlIp.Text; HI_Mod.port = txt_SqlPort.Text; HI_Mod.dbname = txt_DBName.Text; HI_Mod.user = txt_SqlUsr.Text; HI_Mod.password = txt_SqlPwd.Text; if (comb_SqlType.SelectedItem != null) { HI_Mod.sqltype = comb_SqlType.SelectedItem.ToString(); } #endregion //存储 if (spiderName != null)//更新 { if (SI_Bll.Update(SI_Mod)) { } else { MessageBox.Show("配置文件更新失败,未保存到数据库!"); CloseFormFlag = false; } } else//新增 { if (ChangeName != null) { ChangeName(txt_spidername.Text);//返回给主界面 } if (SI_Bll.Add(SI_Mod)) { } else { MessageBox.Show("配置文件更新失败,未添加到数据库!"); CloseFormFlag = false; } } object[] c_temp = HI_Bll.Select(spiderName); if (Convert.ToString(c_temp[0]).Trim() == "") { if (HI_Bll.Add(HI_Mod)) { } else { MessageBox.Show("配置文件更新失败,未添加到数据库!"); CloseFormFlag = false; } } else { if (HI_Bll.Update(HI_Mod)) { } else { MessageBox.Show("配置文件更新失败,未添加到数据库!"); CloseFormFlag = false; } } if (CloseFormFlag) { this.DialogResult = DialogResult.OK; } } else { MessageBox.Show("该任务已经存在!" + txt_spidername.Text); } }