Accessing App.config in a location different from the binary

前端 未结 5 466
予麋鹿
予麋鹿 2020-12-09 06:37

In a .NET Win console application, I would like to access an App.config file in a location different from the console application binary. For example, how can C:\\bin\\Text.

5条回答
  •  隐瞒了意图╮
    2020-12-09 07:25

    using System.Configuration;    
    
    Configuration config =
    ConfigurationManager.OpenExeConfiguration("C:\Test.exe");
    

    You can then access the app settings, connection strings, etc from the config instance. This assumes of course that the config file is properly formatted and your app has read access to the directory. Notice the path is not "C:\Test.exe.config" The method looks for a config file associated with the file you specify. If you specify "C:\Test.exe.config" it will look for "C:\Test.exe.config.config" Kinda lame, but understandable, I guess.

    Reference here: http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openexeconfiguration.aspx

提交回复
热议问题