gpt4 book ai didi

c# - 我无法写入配置文件

转载 作者:太空狗 更新时间:2023-10-29 22:33:35 26 4
gpt4 key购买 nike

我正在使用此类写入配置文件。代码已构建且应用程序已启动,但每次我检查 app.config 时都看不到里面写的任何内容。

可能是什么问题?

代码如下:

public class ConfigSettings
{
private ConfigSettings() { }

public static string ReadSetting(string key)
{
return ConfigurationManager.AppSettings[key];
}

public static void WriteSetting(string key, string value)
{
// load config document for current assembly
XmlDocument doc = loadConfigDocument();

// retrieve appSettings node
XmlNode node = doc.SelectSingleNode("//appSettings");

if (node == null)
throw new InvalidOperationException("appSettings section not found in config file.");

try
{
// select the 'add' element that contains the key
XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));

if (elem != null)
{
// add value for key
elem.SetAttribute("value", value);
}
else
{
// key was not found so create the 'add' element
// and set it's key/value attributes
elem = doc.CreateElement("add");
elem.SetAttribute("key", key);
elem.SetAttribute("value", value);
node.AppendChild(elem);
}
doc.Save(getConfigFilePath());
}
catch
{
throw;
}
}

public static void RemoveSetting(string key)
{
// load config document for current assembly
XmlDocument doc = loadConfigDocument();

// retrieve appSettings node
XmlNode node = doc.SelectSingleNode("//appSettings");

try
{
if (node == null)
throw new InvalidOperationException("appSettings section not found in config file.");
else
{
// remove 'add' element with coresponding key
node.RemoveChild(node.SelectSingleNode(string.Format("//add[@key='{0}']", key)));
doc.Save(getConfigFilePath());
}
}
catch (NullReferenceException e)
{
throw new Exception(string.Format("The key {0} does not exist.", key), e);
}
}

private static XmlDocument loadConfigDocument()
{
XmlDocument doc = null;
try
{
doc = new XmlDocument();
doc.Load(getConfigFilePath());
return doc;
}
catch (System.IO.FileNotFoundException e)
{
throw new Exception("No configuration file found.", e);
}
}

private static string getConfigFilePath()
{
return AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString();
}
}

之后我用它来写入文件:

ConfigSettings.WriteSetting("check", "true");

最佳答案

您可以派生自 System.Configuration.ConfigurationSection 并使其变得非常简单:

public class MyConfigurationSection : System.Configuration.ConfigurationSection
{
[ConfigurationProperty("myProperty")]
public string MyProperty
{
get { return (string)this["myProperty"]; }
set { this["myProperty"] = value; }
}
}

然后在 app/web.config 中添加 configSection。

<configuration>
<configSections>
<section name="myConfiguration" type="MyConfigurationSection, MyAssembly" />
</configSections>

<myConfiguration myProperty="someValue" />
</configuration>

您可以像这样在任何地方获取实例:

ConfigurationManager.GetSection("myConfiguration") as MyConfigurationSection

关于c# - 我无法写入配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10718698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com