I am specifying <appSettings> in my app.config file, I am adding
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings configSource="ShareAppSettings.debug.config"/>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/>
</startup>
</configuration>
ShareAppSettigns.debug.config is my external config file, which I am using on my local machine and I do not want to share it with the rest of my team.
ShareAppSettings.debug.config looks like:
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="clientID" value="11" />
<add key="clientSecret" value="11" />
<add key="tenantID" value="11" />
</appSettings>
Whenever I am trying to debug the main code:
private static List<string> AppCredentials()
{
string clientID = ConfigurationManager.AppSettings["clientID"];
string clientSecret = ConfigurationManager.AppSettings["clientSecret"];
string tenantID = ConfigurationManager.AppSettings["tenantID"];
List<string> appCred = new List<string> { clientID, clientSecret, tenantID };
if (clientID == null)
throw new Exception("ShareAppSettings.Debug.Config file was not provided in this repo.");
return (appCred);
}
For some reason I am not getting values for clientId, slientSecret nor tenantId. This code is a part of grasshopper Add-on for v6 template, and its running on .NET Framework 4.7.1. Whenever I copy the same code into a new C# console of a same framework, the code is built. I would truly appreciate if you could give me suggestions on how to solve this.
What "EnableWindowsFormsHighDpiAutoResizing" means and how can make this work?
Many Thanks
I suggest to use the
fileattribute (corresponding to the AppSettingsSection.File property) instead of theconfigSourceattribute (corresponding to the SectionInformation.ConfigSource property) in theappSettingssection.configSourcedoesn't support other keys in the section, whileappSettingsmay contain other keys, needed by the Application and possibly somewhere else (someone else may add/remove them - for testing purposes or any other reason).The
fileattribute allows instead the presence of other key in theappSettingssection.Your
app.configfile can be:Now the values are accessible by both opening a named configuration section:
appSettings.Settings[]returns a KeyValueConfigurationElementand directly, using ConfigurationManager.AppSettings - a NameValueCollection - which returns the value of the specified key:
As a note, using the
fileattribute, yourShareAppSettings.debug.configdoesn't need (but it's not forbidden) the XML header, it can be just:Secondary note:
you can set the
fileattribute to point to another file at run-time and refresh theappSettingsvalues to updated the configuration.Note that, if a
fileattribute was already set, all values contained in that.configfile are not dismissed, but instead moved to the[Application].exe.configfile, to become part of the<appSettings>section (thus, they are preserved).Another reason why using the
fileattribute may be preferable.