mat*_*uma 13 .net settings configuration app-config
对于我的项目,我有通过项目属性中的"设置"添加的设置.
我很快发现直接编辑app.config文件似乎没有真正更新设置值.似乎我在进行更改然后重新编译时必须查看项目属性.
我想知道...... 处理项目的可自定义设置的最佳和最简单的方法是什么- 认为这对于.Net如何处理它是一个明智的选择 ...对我很羞耻.
是否可以使用其中一个设置,AppSettings, ApplicationSettings或UserSettings来处理这个问题?
将我的设置写入自定义配置文件并自行处理是否更好?
现在...... 我正在寻找最快的解决方案!
我的环境是C#,.Net 3.5和Visual Studio 2008.
更新
我正在尝试执行以下操作:
protected override void Save()
{
Properties.Settings.Default.EmailDisplayName = this.ddEmailDisplayName.Text;
Properties.Settings.Default.Save();
}
Run Code Online (Sandbox Code Playgroud)
编译时给我一个只读错误.
小智 6
我也试图解决这个需求,我现在有一个漂亮漂亮的 ConsoleApplication,我想分享:(App.config)
您将看到的是:
玩得开心!
public void UpdateProperty(string key, string value)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;
// update SaveBeforeExit
config.AppSettings.Settings[key].Value = value;
Console.Write("...Configuration updated: key "+key+", value: "+value+"...");
//save the file
config.Save(ConfigurationSaveMode.Modified);
Console.Write("...saved Configuration...");
//relaod the section you modified
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
Console.Write("...Configuration Section refreshed...");
}
public void ReadAppSettingsProperty()
{
try
{
var section = ConfigurationManager.GetSection("applicationSettings");
// Get the AppSettings section.
NameValueCollection appSettings = ConfigurationManager.AppSettings;
// Get the AppSettings section elements.
Console.WriteLine();
Console.WriteLine("Using AppSettings property.");
Console.WriteLine("Application settings:");
if (appSettings.Count == 0)
{
Console.WriteLine("[ReadAppSettings: {0}]", "AppSettings is empty Use GetSection command first.");
}
for (int i = 0; i < appSettings.Count; i++)
{
Console.WriteLine("#{0} Key: {1} Value: {2}",
i, appSettings.GetKey(i), appSettings[i]);
}
}
catch (ConfigurationErrorsException e)
{
Console.WriteLine("[ReadAppSettings: {0}]", e.ToString());
}
}
public void updateAppSettingProperty(string key, string value)
{
// Get the application configuration file.
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string sectionName = "appSettings";
config.AppSettings.Settings.Remove(key);
config.AppSettings.Settings.Add(key, value);
SaveConfigFile(config);
}
public void insertAppSettingProperty(string key, string value)
{
// Get the application configuration file.
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string sectionName = "appSettings";
config.AppSettings.Settings.Add(key, value);
SaveConfigFile(config);
}
public void deleteAppSettingProperty(string key)
{
// Get the application configuration file.
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove(key);
SaveConfigFile(config);
}
private static void SaveConfigFile(System.Configuration.Configuration config)
{
string sectionName = "appSettings";
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of the changed section. This
// makes the new values available for reading.
ConfigurationManager.RefreshSection(sectionName);
// Get the AppSettings section.
AppSettingsSection appSettingSection =
(AppSettingsSection)config.GetSection(sectionName);
Console.WriteLine();
Console.WriteLine("Using GetSection(string).");
Console.WriteLine("AppSettings section:");
Console.WriteLine(appSettingSection.SectionInformation.GetRawXml());
}
}
Run Code Online (Sandbox Code Playgroud)
配置文件如下所示:
<configuration>
<configSections>
</configSections>
<appSettings>
<add key="aNewKey1" value="aNewValue1" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
好吧,所以我在使用此解决方案时对 AppSettings 没有任何问题!玩得开心... ;-) !