C# Encryption ConnectionString返回对象引用未设置为实例

C# Encryption ConnectionString返回对象引用未设置为实例,c#,.net,encryption,connection-string,C#,.net,Encryption,Connection String,我使用vs 20124.5我使用加密来加密我的连接字符串,但是当我调用 connStr = ConfigurationManager.ConnectionStrings["SnOracle"].ConnectionString; 我发现这个错误对象没有设置为对象的实例 我正在获取没有ConnectionStringSection的MyApp.exe.Config文件的路径 ,但加密的MyApp.exe.Config.Config确实具有ConnectionStringsSection.Me

我使用vs 20124.5我使用加密来加密我的连接字符串,但是当我调用

  connStr = ConfigurationManager.ConnectionStrings["SnOracle"].ConnectionString;
我发现这个错误对象没有设置为对象的实例

我正在获取没有ConnectionStringSection的MyApp.exe.Config文件的路径 ,但加密的MyApp.exe.Config.Config确实具有ConnectionStringsSection.Meybe我需要将路径设置为 MyApp.exe.Config.Config由我自己或AppDomain.CurrentDomain.SetupInformation.ConfigurationFile创建;需要返回加密文件的路径吗

如何从加密的MyApp.exe.Config.Config文件中读取ConnectionStringsSection

我做错了什么

我的代码:

    string path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
// This return path to-> MyApp.exe.Config   file that dont have  ConnectionStringsSection
//But Encrypted  MyApp.exe.Config.config DO have  ConnectionStringsSection
  Configuration config = ConfigurationManager.OpenExeConfiguration(path);
        ConnectionStringsSection section = config.GetSection("connectionStrings") as ConnectionStringsSection;

                if (!section.SectionInformation.IsProtected)
                {
                    // Encrypt the section.
                    section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");

                }
                step = "9";
                // Remove encryption.
                // section.SectionInformation.UnprotectSection();
                // Save the current configuration.

                config.Save();
                step = "9a";
                string connStr = "";
                try
                {
                     //here i getting the error
                    connStr = ConfigurationManager.ConnectionStrings["SnOracle"].ConnectionString;

                }
                catch (Exception ex)
                {
                    //here afert it fails i insert it hard coded
                    connStr = "Data Source=xxx;User Id=xxx;Password=xxx";

                }
MyApp.exe.Config:

<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>
  <appSettings>

  </appSettings>
</configuration>
MyApp.exe.Config.Config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
        <EncryptedData>
            <CipherData>
                <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAffcYl/Mg5kKs24jULobCqAQAAAACAAAAAAADZgAAwAAAABAAAADYu948udrIvfvh5iP1Atz/AAAAAASAAACgfgLks14VfblBt3TX0E5ZnkwAAAASK+Jz3H9lx7DWVEQGXVS2HhqURpMbgFenk6v/lO6lMOk7Gv+tEhEmLs/zNApOaYNFAAAAJ1UfmT1bhg4zzVFv9/RcjNKjRzL</CipherValue>
            </CipherData>
        </EncryptedData>
    </connectionStrings>
</configuration>

我知道这是Null ex。我正在使用加密来加密我的连接字符串,而你向我发送指向NullReferenceException链接的链接?你是认真的吗?问题是我无法读取加密的连接字符串,并且我理解为什么我获取的未加密文件路径是空的。