以编程方式将密钥添加到 App.config C#
Add key to App.config programmatically C#
我正在尝试以编程方式将密钥添加并保存到我的 App.config 文件中。我的代码在下面,我见过很多这样的不同例子,但我的代码不起作用。我正在尝试添加一个全新的密钥,而不是修改现有的。这是一个控制台应用程序,我确实确保添加了对 System.configuration.
的引用
Program.cs
using System;
using System.Linq;
using System.Text;
using System.Configuration;
namespace AddValuesToConfig
{
class Program
{
static void Main(string[] args)
{
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("key1", "value");
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
}
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
</appSettings>
</configuration>
很可能您 运行 这段代码在 Visual Studio 的调试器下。事情是当你这样做时,Visual Studio 实际上 运行 不是 YourApp.exe,而是 YourApp.vshost.exe 文件。这意味着您要将密钥添加到 YourApp.vshost.exe.config 文件而不是 YourApp.exe.config。在没有调试器的情况下尝试 运行,它应该可以工作。
编辑以回答下面的评论。主题发起人声称 VS 在调试时甚至不写入 .vshost.exe.config,我认为这是真的。然而,很少有调查表明它确实写入了 .vshost.config.exe 文件,但是当您停止调试时,它会用您的 .exe.config 覆盖您的 .vshost.exe.config 的内容。所以在调试会话结束后,您可能认为它根本没有写任何东西。但是,如果您在 config.Save() 语句之后立即放置断点并打开 .vshost.exe.config 文件 - 您将在此处看到更改。
写:
public static bool SetAppSettings<TType>(string key, TType value)
{
try
{
if (string.IsNullOrEmpty(key))
return false;
Configuration appConfig = ConfigurationManager.OpenExeConfiguration(GetCurrentApplicationPath());
AppSettingsSection appSettings = (AppSettingsSection)appConfig.GetSection("appSettings");
if (appSettings.Settings[key] == null)
appSettings.Settings.Add(key, value.ToString());
else
appSettings.Settings[key].Value = value.ToString();
appConfig.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
return true;
}
catch
{
return false;
}
}
阅读:
public static TType GetAppSettings<TType>(string key, TType defaultValue = default(TType))
{
try
{
if (string.IsNullOrEmpty(key))
return defaultValue;
AppSettingsReader appSettings = new AppSettingsReader();
return (TType)appSettings.GetValue(key, typeof(TType));
}
catch
{
return defaultValue;
}
}
我正在尝试以编程方式将密钥添加并保存到我的 App.config 文件中。我的代码在下面,我见过很多这样的不同例子,但我的代码不起作用。我正在尝试添加一个全新的密钥,而不是修改现有的。这是一个控制台应用程序,我确实确保添加了对 System.configuration.
的引用Program.cs
using System;
using System.Linq;
using System.Text;
using System.Configuration;
namespace AddValuesToConfig
{
class Program
{
static void Main(string[] args)
{
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("key1", "value");
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
}
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
</appSettings>
</configuration>
很可能您 运行 这段代码在 Visual Studio 的调试器下。事情是当你这样做时,Visual Studio 实际上 运行 不是 YourApp.exe,而是 YourApp.vshost.exe 文件。这意味着您要将密钥添加到 YourApp.vshost.exe.config 文件而不是 YourApp.exe.config。在没有调试器的情况下尝试 运行,它应该可以工作。
编辑以回答下面的评论。主题发起人声称 VS 在调试时甚至不写入 .vshost.exe.config,我认为这是真的。然而,很少有调查表明它确实写入了 .vshost.config.exe 文件,但是当您停止调试时,它会用您的 .exe.config 覆盖您的 .vshost.exe.config 的内容。所以在调试会话结束后,您可能认为它根本没有写任何东西。但是,如果您在 config.Save() 语句之后立即放置断点并打开 .vshost.exe.config 文件 - 您将在此处看到更改。
写:
public static bool SetAppSettings<TType>(string key, TType value)
{
try
{
if (string.IsNullOrEmpty(key))
return false;
Configuration appConfig = ConfigurationManager.OpenExeConfiguration(GetCurrentApplicationPath());
AppSettingsSection appSettings = (AppSettingsSection)appConfig.GetSection("appSettings");
if (appSettings.Settings[key] == null)
appSettings.Settings.Add(key, value.ToString());
else
appSettings.Settings[key].Value = value.ToString();
appConfig.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
return true;
}
catch
{
return false;
}
}
阅读:
public static TType GetAppSettings<TType>(string key, TType defaultValue = default(TType))
{
try
{
if (string.IsNullOrEmpty(key))
return defaultValue;
AppSettingsReader appSettings = new AppSettingsReader();
return (TType)appSettings.GetValue(key, typeof(TType));
}
catch
{
return defaultValue;
}
}