Map .exe.config file to datagridview and manage settings

I am working on a graphical interface that can manipulate xml files through a datagridview and save it to the user's destination. This program also has a .exe.config file, in which I would also like to be able to freely edit inside a datagridview, since it is much more convenient than manually entering the file and changing the values ​​accordingly.

I tried declaring a dataset and I realized that the .exe.config file was just an xml file, but this code does not work:

dataSet1.ReadXml(configpath); bindingSource1.DataSource = dataSet1.Tables[0]; dataGridView1.DataSource = bindingSource1; 

Datagridview is empty when I ran it, and I confirmed that the file path is correct, and there was no exception when I debugged the code, whereas for other xml files that I open in the GUI, it works fine with the displayed data. Maybe readxml () only supports ... legit xml files, not xml configuration files? I tried searching the Internet and looking for answers, but all I had was streams related to changing the settings by manually accessing the XML file and changing the values ​​(which I already know). I want the user to be able to do what they want to do with the data, and then save it. The .exe.config options may also be for another program, but this is essentially an xml configuration file. I thought that there is not much in this problem, because the settings are usually static, and if they are changed, it is quite easy to do it manually.

Summarizing,

I am looking for a way to open any .exe.config file, display it in a datagridview, allow the user to be able to manipulate the data values ​​inside, and then save the file, overwriting the previous data settings.

Any help is appreciated. Thank you in advance!

  • tf.rz (.NET 3.5 SP1, Visual Studio 2008 C #)

EDIT: I will download a working example of the xml file I created: I want the program to be able to go to the .exe.config file, then open it and display it as shown here, where the parameter names are columns and the values ​​are in datagridview cells. Unfortunately, I am not on my home computer to do this.

+6
source share
1 answer

This is what I used to download and manage the configuration file. You can change the loadAppSettings and loadConnStrings to suit your needs.

 using System; using System.Collections.Specialized; using System.Linq; using System.Text; using System.Xml; using System.IO; namespace GenericManagementClasses { public class ConfigFile { private string m_ConfigFilePath; private XmlDocument m_XmlDoc; private FileStream fIn; private StreamReader sr; private StreamWriter sw; private OrderedDictionary m_AppSettings; private OrderedDictionary m_ConnectionStrings; private XmlNode m_AppSettingsNode; private XmlNode m_ConnectionStringsNode; #region "Properties" public String Path { get { return m_ConfigFilePath; } } public OrderedDictionary AppSettings { get { return m_AppSettings; } } public OrderedDictionary ConnectionStrings { get { return m_ConnectionStrings; } } #endregion #region "Constructors" /// <summary> /// Default constructor - declared private so that you can't instantiate an empty ConfigFile object /// <code>ConfigFile cfg = new ConfigFile()</code> will result in a NotImplemented exception /// </summary> private ConfigFile() { throw new NotImplementedException("No default constructor for the ConfigFile class"); } /// <summary> /// Public constructor /// <example>ConfigFile cfg = new ConfigFile(@"c:\MyApp\MyApp.exe.config");</example> /// </summary> /// <param name="ConfigFilePath">The path to the configuration file</param> public ConfigFile(string ConfigFilePath) { //Check to see if the file exists if (File.Exists(ConfigFilePath)){ //Initialise the XmlDocument to hold the config file m_XmlDoc = new XmlDocument(); //Store the path to the config file m_ConfigFilePath = ConfigFilePath; //FileStream to get the contents out of the file fIn = new FileStream(m_ConfigFilePath, FileMode.Open, FileAccess.ReadWrite); //StreamReader to read the FileStream sr = new StreamReader(fIn); //StreamWriter to write to the FileStream sw = new StreamWriter(fIn); //Try and load the XML from the file stream try { m_XmlDoc.LoadXml(sr.ReadToEnd()); m_AppSettingsNode = m_XmlDoc.GetElementsByTagName("appSettings")[0]; m_ConnectionStringsNode = m_XmlDoc.GetElementsByTagName("connectionStrings")[0]; loadAppSettings(); loadConnStrings(); } catch (Exception ex) { //If it went pear shaped, throw the exception upwards throw ex; } } else //If the file doesn't exist, throw a FileNotFound exception { throw new FileNotFoundException(ConfigFilePath + " does not exist"); } } #endregion private void loadAppSettings() { m_AppSettings = new OrderedDictionary(); XmlNodeList nl = m_AppSettingsNode.SelectNodes("add"); foreach (XmlNode node in nl) { m_AppSettings.Add(node.Attributes["key"].Value, node.Attributes["value"].Value); } } private void loadConnStrings() { m_ConnectionStrings = new OrderedDictionary(); XmlNodeList nl = m_ConnectionStringsNode.SelectNodes("add"); foreach (XmlNode node in nl) { m_ConnectionStrings.Add(node.Attributes["name"].Value, node.Attributes["connectionString"].Value); } } public void setAppSetting(string name, string newValue) { if (!m_AppSettings.Contains(name)) { throw new Exception(String.Format("Setting {0} does not exist in {1}", name, m_ConfigFilePath)); } else { m_AppSettings[name] = newValue; m_XmlDoc.SelectSingleNode(String.Format(@"//appSettings/add[@key='{0}']",name)).Attributes["value"].Value = newValue; fIn.SetLength(0); sw.Write(m_XmlDoc.InnerXml); sw.Flush(); } } #region "Static Methods" /// <summary> /// Static method to return a ConfigFile object /// <example>ConfigFile cfg = ConfigFile.LoadConfigFile(@c:\MyApp\MyApp.exe.config");"</example> /// </summary> /// <param name="ConfigFilePath">Path to the configuration file to load</param> /// <returns></returns> public static ConfigFile LoadConfigFile(string ConfigFilePath) { return new ConfigFile(ConfigFilePath); } #endregion } } 
+2
source

Source: https://habr.com/ru/post/889395/


All Articles