C# 修改配置节会将该节保存到其他配置文件中
我有一个C# 修改配置节会将该节保存到其他配置文件中,c#,encryption,configuration,app-config,C#,Encryption,Configuration,App Config,我有一个myapp.somenamespace.exe.config文件,其中包含一个ConnectionString部分,我需要对其进行加密。此外,还有一些其他配置设置,我想原封不动。所以我写了一个小工具,可以做到这一点: class Program { static void Main(string[] args) { EncryptSection("myapp.somenamespace.exe.config", "connectionStrings");
myapp.somenamespace.exe.config
文件,其中包含一个ConnectionString部分,我需要对其进行加密。此外,还有一些其他配置设置,我想原封不动。所以我写了一个小工具,可以做到这一点:
class Program
{
static void Main(string[] args)
{
EncryptSection("myapp.somenamespace.exe.config", "connectionStrings");
}
static void EncryptSection(string fileName, string sectionName)
{
var config = ConfigurationManager.OpenExeConfiguration(fileName);
var section = config.GetSection(sectionName);
if (section.SectionInformation.IsProtected) return;
secction.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
config.Save();
}
}
结果,它创建了一个名为myapp.somenamespace.exe.config.config
的新配置文件,添加了一个重复的.config
扩展名,该扩展名只包含加密的部分。它不修改原始配置文件
你知道为什么会有这种奇怪的行为吗?我怎样才能解决它?改变这一行:
EncryptSection("myapp.somenamespace.exe.config", "connectionStrings");
为此:
EncryptSection("myapp.somenamespace.exe", "connectionStrings");
声明第一个参数实际上是可执行(exe)文件的路径,因此它在保存过程中附加了一个
.config
,Andrey,我很高兴能提供帮助!