2013-04-04 36 views
2

我有myapp.somenamespace.exe.config文件带有connectionStrings节,我需要加密。还有一些我想要的完整的其他配置设置。所以我写了这个小工具,它会做到这一点:修改配置节将该节保存到不同的配置文件

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扩展,仅包含加密部分。它不是修改原始配置文件。

任何想法为什么这样一个奇怪的行为,我怎么能解决这个问题?

回答

1

改变这一行:

EncryptSection("myapp.somenamespace.exe.config", "connectionStrings"); 

这样:

EncryptSection("myapp.somenamespace.exe", "connectionStrings"); 

the documentation on MSDN指出,第一个参数其实he path of the executable (exe) file,因此它在Save套结上附加.config

+0

太好了,非常感谢! – Andrey 2013-04-11 14:48:38

+0

@Andrey,我很高兴我可以帮助! – 2013-04-11 14:49:03