Accessing App.config in a location different from the binary

前端 未结 5 465
予麋鹿
予麋鹿 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:44

    It appears that you can use the AppDomain.SetData method to achieve this. The documentation states:

    You cannot insert or modify system entries with this method.

    Regardless, doing so does appear to work. The documentation for the AppDomain.GetData method lists the system entries available, of interest is the "APP_CONFIG_FILE" entry.

    If we set the "APP_CONFIG_FILE" before any application settings are used, we can modify where the app.config is loaded from. For example:

    public class Program
    {
        public static void Main()
        {
            AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\Temp\test.config");
            //...
        }
    }
    

    I found this solution documented in this blog and a more complete answer (to a related question) can be found here.

提交回复
热议问题