ConfigurationManager.OpenExeConfiguration - loads the wrong file?

后端 未结 4 899
旧巷少年郎
旧巷少年郎 2020-12-01 13:42

I have added multiple app.config (each with a differet name) files to a project, and set them to copy to the output directory on each build.

I try and access the con

4条回答
  •  星月不相逢
    2020-12-01 14:36

    using System.Reflection;
    
    try
    {
        Uri UriAssemblyFolder = new Uri(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase));
        string appPath = UriAssemblyFolder.LocalPath;
    
        //Open the configuration file and retrieve 
        //the connectionStrings section.
        Configuration config = ConfigurationManager.
            OpenExeConfiguration(appPath + @"\" + exeConfigName);
    
        ConnectionStringsSection section =
            config.GetSection("connectionStrings")
            as ConnectionStringsSection;
    }
    

    At least, this is the method I use when encrypting and decrypting the connectionStrings section for my console/GUI apps. exeConfigName is the name of the executable including the .exe.

提交回复
热议问题