C#修改*.exe.config配置文件已经搞定,不知道有没有刷新缓存中的配置文件的方法,求大神解答
这是我现在用的方法,感觉挺麻烦的,不知道各位大神有没更好的方法,找到了一个读取的方法,就是加载config文件然后读取,但是感觉不好,不能用ConfigurationMa...
这是我现在用的方法,感觉挺麻烦的,不知道各位大神有没更好的方法,找到了一个读取的方法,就是加载config文件然后读取,但是感觉不好,不能用ConfigurationManager.ConnectionStrings[“connectionString”]读取
public void saveAPPCONFIG(string skeyname, string skeyvalue)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(_configPath);
XmlNode xConfiguration = xmldoc["configuration"];
if (xConfiguration["connectionStrings"] == null)
{
XmlNode xConnectionStrings = xmldoc.CreateElement("connectionStrings");
xConfiguration.AppendChild(xConnectionStrings); //向configuration添加connectionStrings节点
XmlNode xConnectionString = xmldoc.CreateElement("add");
XmlAttribute xmlAttrName;
xmlAttrName = xmldoc.CreateAttribute("name");
xmlAttrName.Value = skeyname;
XmlAttribute xmlAttrConnectionString;
xmlAttrConnectionString = xmldoc.CreateAttribute("connectionString");
xmlAttrConnectionString.Value = skeyvalue;
xConnectionString.Attributes.SetNamedItem(xmlAttrName);
xConnectionString.Attributes.SetNamedItem(xmlAttrConnectionString);
xConnectionStrings.AppendChild(xConnectionString);//向xConnectionStrings添加xConnectionString节点
}
xmldoc.Save(_configPath);
} 展开
public void saveAPPCONFIG(string skeyname, string skeyvalue)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(_configPath);
XmlNode xConfiguration = xmldoc["configuration"];
if (xConfiguration["connectionStrings"] == null)
{
XmlNode xConnectionStrings = xmldoc.CreateElement("connectionStrings");
xConfiguration.AppendChild(xConnectionStrings); //向configuration添加connectionStrings节点
XmlNode xConnectionString = xmldoc.CreateElement("add");
XmlAttribute xmlAttrName;
xmlAttrName = xmldoc.CreateAttribute("name");
xmlAttrName.Value = skeyname;
XmlAttribute xmlAttrConnectionString;
xmlAttrConnectionString = xmldoc.CreateAttribute("connectionString");
xmlAttrConnectionString.Value = skeyvalue;
xConnectionString.Attributes.SetNamedItem(xmlAttrName);
xConnectionString.Attributes.SetNamedItem(xmlAttrConnectionString);
xConnectionStrings.AppendChild(xConnectionString);//向xConnectionStrings添加xConnectionString节点
}
xmldoc.Save(_configPath);
} 展开
1个回答
展开全部
private static void UpdateAppConfig(string newKey, string newValue)
{
bool isModified = false;
foreach (string key in ConfigurationManager.AppSettings)
{
if (key == newKey)
{
isModified = true;
}
}
// Open App.Config of executable
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// You need to remove the old settings object before you can replace it
if (isModified)
{
config.AppSettings.Settings.Remove(newKey);
}
// Add an Application Setting.
config.AppSettings.Settings.Add(newKey, newValue);
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
}
之后直接用ConfigurationManager.AppSettings[key]读取就行了。
更多追问追答
追问
这个是修改*.vshost.exe.config的,但是ConfigurationManager读取的是exe.config的,这个我早就试过了,不知道修改*.vshost.exe.config有什么用,一百度全是这个
追答
vshost那个文件是VS里调试用的,你用上面的代码,然后直接运行你生成出来的exe就能看到exe.config的更改了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询