Dynamically assigning The Configuration file path

Though it can be a real securuity hazard to keep the Configuation file out of the scopem where the application resides since it may cause unavailability of file sometimes. But somehow we need to do that and here is the way we can do this in the Windows Apps.

1. just paste following line in the Program.Cs file before Application.Run(new Form1());
AppDomain.CurrentDomain.SetData("AppConfig", "D:\\Test.config");

2. Now the data in the appsettings and you'll find the data you require for instance
string confData = ConfigurationManager.AppSettings["FilePath"];

4. The above way will permanantly change the path of the config File. If the requirement is to just address another file and then switching batch to the orginal one, then here is the code

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFilePath, ConfigurationUserLevel.None);
AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings");
string MySetting = section.Settings["MyName"].Value;
MessageBox.Show(ConfigurationManager.AppSettings["MyName"].ToString());

Now the above code will allow you to keep the object of AppSettings and Work with the original Configuration file simultaneously.

Comments

Popular posts from this blog

SPFx: Develop using SharePoint Framework without Installing all the dependecies.

SharePoint Online: Elevated Permissions....with love

Powershell: Filling up an Existing Excel Sheet with data from SQL Server