private void btnLoad_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(txtSourceCodePath.Text) || String.IsNullOrEmpty(projComboBox.Text)) { MessageBox.Show("The Seetings are not complete", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); mainTabCtrl.SelectedIndex = 0; return; } ListFiles configFiles = new ListFiles(txtSourceCodePath.Text, projComboBox.Text); FileInfo legoWebConfig = new FileInfo(configFiles.GetFiles().ElementAt(0)); try { XmlReadWrite reader = new XmlReadWrite(legoWebConfig.FullName); var config = reader.ReadConfig(); txtSource.Text = config.DataSource; txtInit.Text = config.InitialCatalog; txtBase.Text = config.BaseCatalog; } catch (FileNotFoundException) { MessageBox.Show("The selected project is not valid.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); mainTabCtrl.SelectedIndex = 0; projComboBox.Focus(); } }
private void ChangeConfig() { try { string path = txtSourceCodePath.Text; if (!CheckPath(ref path)) { MessageBox.Show("The selected folder doesn't contain MasterMindAgile!"); txtSourceCodePath.Clear(); return; } if (projComboBox.SelectedIndex == 0) { MessageBox.Show("Select your project!"); return; } StringBuilder successfulMsg = new StringBuilder(); StringBuilder unSuccessfulMsg = new StringBuilder(); ListFiles projects = new ListFiles(txtSourceCodePath.Text, projComboBox.Text); foreach (var item in projects.GetFiles()) { if (!File.Exists(item)) { continue; } try { FileInfo file = new FileInfo(item); file.IsReadOnly = false; var doc = XDocument.Load(item); var appSettings = doc.Root.Element("appSettings"); var adds = appSettings.Descendants("add").ToList(); if (adds != null) { var dsTag = adds.Where(x => x.Attribute("key").Value == "DataSource"); var initTag = adds.Where(x => x.Attribute("key").Value == "InitialCatalog"); var baseTag = adds.Where(x => x.Attribute("key").Value == "BaseCatalog"); if (dsTag != null && initTag != null && baseTag != null) { dsTag.First().LastAttribute.Value = txtSource.Text; initTag.First().LastAttribute.Value = txtInit.Text; baseTag.First().LastAttribute.Value = txtBase.Text; } doc.Save(item); } successfulMsg.Append(new DirectoryInfo(Path.GetDirectoryName(item)).Name + Environment.NewLine); } catch (Exception) { unSuccessfulMsg.Append(new DirectoryInfo(Path.GetDirectoryName(item)).Name + Environment.NewLine); } } string message = ""; if (unSuccessfulMsg.Length > 0 && successfulMsg.Length > 0) { message = String.Format("Successful changes:{1}{0}Unsuccessful changes:{1}{2}", successfulMsg.ToString() + Environment.NewLine, Environment.NewLine + new string('-', 30) + Environment.NewLine, unSuccessfulMsg); } else if (unSuccessfulMsg.Length > 0 && successfulMsg.Length == 0) { message = "Changing Web.Config failed."; } else { message = "Web.configs changed successfully."; } MessageBox.Show(message); SaveConfigs(); } catch (Exception ex) { MessageBox.Show("Changing Web.Config failed : " + ex.Message); } }