自定义config文件读写 自定义一个config文件夹,包含不同的配置文件,如果实现读写,如discuz!nt一定,有没有现成的类和简单的一些方法 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 自定义一个config文件夹,包含网站运行的多个的配置文件,如何实现读写,如discuz!nt一样,所有的配置文件都放在config文件夹下,并不是我们常用的web.confg文件, 有没有现成的类和简单的一些方法,直接实现看了一个NT的,好像有点复杂=============================今天的输入法不知是怎么了老出问题============================= 那就是操作xml文件了。可以把操作xml文件的方法封装了。建议使用xpath这个东西蛮方便的。 引用System.Xml命名空间,参考一下这段代码: private void WriteConfigFile(string FilePath) { FilePath = string.Format("{0}\\{1}", CurrentPath.Substring(0, CurrentPath.LastIndexOf('\\')), FilePath); if (!File.Exists(FilePath)) { MessageBox.Show("Web配置文件丢失,请尝试重新安装解决该问题。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } XmlDocument document = new XmlDocument(); document.Load(FilePath); XmlNode appSettingNode = document.SelectSingleNode("/configuration/connectionStrings"); XmlNode dbConnectionNode = document.SelectSingleNode("/configuration/connectionStrings/add[@name='" + ConnectionStringNodeName + "']"); if (dbConnectionNode != null) { dbConnectionNode.Attributes["connectionString"].Value = GetConnectionString(); dbConnectionNode.Attributes["providerName"].Value = ProviderName; } else { XmlElement elementAdd = document.CreateElement("add"); XmlAttribute keyAttribute = document.CreateAttribute("name"); keyAttribute.Value = ConnectionStringNodeName; XmlAttribute valueAttribute = document.CreateAttribute("connectionString"); valueAttribute.Value = this.m_connection.ConnectionString; XmlAttribute providerAttribute = document.CreateAttribute("providerName"); providerAttribute.Value = ProviderName; elementAdd.Attributes.Append(keyAttribute); elementAdd.Attributes.Append(valueAttribute); elementAdd.Attributes.Append(providerAttribute); appSettingNode.AppendChild(elementAdd); } try { document.Save(FilePath); } catch (Exception ex) { throw ex; } } 那应该就算是普通的读取XML文本文件的操作了吧。 给你个简单例子,注意所保存的对象必须支持序列化:using System;using System.Collections.Generic;using System.Text;using System.Reflection;using System.IO;using System.Xml;using System.Xml.Serialization;namespace BNCore.Common.Config{ public class ConfigurationXmlReadWriteHelper { public static void SaveConfig(string path, object mObject) { StreamWriter XmlWriter = new StreamWriter(path, false, System.Text.Encoding.GetEncoding("gb2312")); XmlWriter.Write(GetXML(mObject)); XmlWriter.Close(); } private static string GetXML(object mObject) { XmlSerializer oSerializer = new XmlSerializer(mObject.GetType()); StringWriter oStringWriter = new StringWriter(); oSerializer.Serialize(oStringWriter, mObject); return oStringWriter.ToString(); } private static object LoadXML(System.Type mClass, string XML) { XmlSerializer oSerializer = new XmlSerializer(mClass); StringReader oStringReader = new StringReader(XML); return oSerializer.Deserialize(oStringReader); } public static object LoadConfig(System.Type mClass, string path) { StreamReader XmlReader = new StreamReader(path, System.Text.Encoding.GetEncoding("gb2312")); object obj = LoadXML(mClass, XmlReader.ReadToEnd()); XmlReader.Close(); return obj; } }} webservice调用 关于使用三目运算时无法确定条件表达式的问题 Session问题(急)(急) DataList控件的ItemTemplate中嵌入Table控件,在Table中动态的生成TextBox,不能实现,看代码。 改存储过程为一般语句 请叫关于AutoPostBack=true不刷新的问题 如何将GridView 中的内容导出到Excel里面,并且设置Excel里面的 题头 并在下面加上打印时间 想不通,求教 Winson.Framework 1.0发布! Gridview内容转成EXCEL问题 ImageButton 的那点小事情; WebPart使用问题
自定义一个config文件夹,包含网站运行的多个的配置文件,如何实现读写,如discuz!nt一样,所有的配置文件都放在config文件夹下,并不是我们常用的web.confg文件, 有没有现成的类和简单的一些方法,直接实现看了一个NT的,好像有点复杂
=============================
今天的输入法不知是怎么了老出问题
=============================
可以把操作xml文件的方法封装了。
建议使用xpath这个东西蛮方便的。
{
FilePath = string.Format("{0}\\{1}", CurrentPath.Substring(0, CurrentPath.LastIndexOf('\\')), FilePath);
if (!File.Exists(FilePath))
{
MessageBox.Show("Web配置文件丢失,请尝试重新安装解决该问题。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
} XmlDocument document = new XmlDocument();
document.Load(FilePath);
XmlNode appSettingNode = document.SelectSingleNode("/configuration/connectionStrings");
XmlNode dbConnectionNode = document.SelectSingleNode("/configuration/connectionStrings/add[@name='" + ConnectionStringNodeName + "']");
if (dbConnectionNode != null)
{
dbConnectionNode.Attributes["connectionString"].Value = GetConnectionString();
dbConnectionNode.Attributes["providerName"].Value = ProviderName;
}
else
{
XmlElement elementAdd = document.CreateElement("add");
XmlAttribute keyAttribute = document.CreateAttribute("name");
keyAttribute.Value = ConnectionStringNodeName;
XmlAttribute valueAttribute = document.CreateAttribute("connectionString");
valueAttribute.Value = this.m_connection.ConnectionString;
XmlAttribute providerAttribute = document.CreateAttribute("providerName");
providerAttribute.Value = ProviderName;
elementAdd.Attributes.Append(keyAttribute);
elementAdd.Attributes.Append(valueAttribute);
elementAdd.Attributes.Append(providerAttribute);
appSettingNode.AppendChild(elementAdd);
}
try
{
document.Save(FilePath);
}
catch (Exception ex)
{
throw ex;
}
}
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Xml;
using System.Xml.Serialization;namespace BNCore.Common.Config
{
public class ConfigurationXmlReadWriteHelper
{
public static void SaveConfig(string path, object mObject)
{ StreamWriter XmlWriter = new StreamWriter(path, false, System.Text.Encoding.GetEncoding("gb2312"));
XmlWriter.Write(GetXML(mObject));
XmlWriter.Close();
} private static string GetXML(object mObject)
{
XmlSerializer oSerializer = new XmlSerializer(mObject.GetType());
StringWriter oStringWriter = new StringWriter();
oSerializer.Serialize(oStringWriter, mObject);
return oStringWriter.ToString();
}
private static object LoadXML(System.Type mClass, string XML)
{
XmlSerializer oSerializer = new XmlSerializer(mClass);
StringReader oStringReader = new StringReader(XML);
return oSerializer.Deserialize(oStringReader);
}
public static object LoadConfig(System.Type mClass, string path)
{
StreamReader XmlReader = new StreamReader(path, System.Text.Encoding.GetEncoding("gb2312"));
object obj = LoadXML(mClass, XmlReader.ReadToEnd());
XmlReader.Close();
return obj;
}
}
}