Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 修改配置节会将该节保存到其他配置文件中_C#_Encryption_Configuration_App Config - Fatal编程技术网

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,我很高兴能提供帮助!