问题描述
||
我正在使用一个GUI,该GUI能够通过datagridview处理xml文件并将其保存到用户选择的目标位置。该程序还具有一个.exe.config文件,我也希望能够在该文件中自由编辑datagridview,因为它比让用户手动进入文件并相应地更改值要方便得多。
我已经尝试声明一个数据集,并且最初认为.exe.config文件只是一个xml文件,但是此代码不起作用:
dataSet1.readxml(configpath);
bindingSource1.DataSource = dataSet1.Tables[0];
dataGridView1.DataSource = bindingSource1;
当我运行它时datagridview为空,并且我确认文件路径正确,并且在调试代码时也没有异常,而对于在GUI中打开的其他xml文件,它与显示的数据完全兼容。也许readxml()仅支持合法的xml文件,而不支持xml配置文件?我尝试使用谷歌搜索并寻找一些答案,但是我所得到的都是与通过手动访问xml文件并更改值(我已经知道的东西)来更改设置有关的线程。我希望能够让用户对数据做他们想做的事,然后保存它。 .exe.config设置可能也适用于另一个程序,但它实际上是一个xml配置文件。我发现网上没有太多针对此特定问题的信息,因为设置通常是静态的,并且如果更改了设置,则手动进行非常容易。
总结一下,
我正在寻找一种方法来打开任何.exe.config文件,将其显示在datagridview中,使用户能够操纵内部的数据值,然后保存该文件,覆盖以前的数据设置。
任何帮助表示赞赏。
先感谢您!
tf.rz(.NET 3.5 SP1,Visual Studio 2008 C#)
编辑:我将上传一个我创建的xml文件的工作示例:我有点希望该程序能够导航到.exe.config文件,然后将其打开并像这样显示,其中设置名称是列值在datagridview的单元格中。不幸的是,我不在家用计算机上能够执行此操作。
解决方法
这就是我用来加载和操作配置文件的内容。您可能需要更改
loadAppSettings
和loadConnStrings
方法以适合您的需求。
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
}
}