0

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

enter image description here

lMia
  • 11
  • 1

1 Answers1

0

I suggest to use the file attribute (corresponding to the AppSettingsSection.File property) instead of the configSource attribute (corresponding to the SectionInformation.ConfigSource property) in the appSettings section.

  • configSource doesn't support other keys in the section, while appSettings may 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 file attribute allows instead the presence of other key in the appSettings section.

Your app.config file can be:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings file="ShareAppSettings.debug.config">
        <add key="DpiAwareness" value="PerMonitorV2"/>
    </appSettings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/>
    </startup>
</configuration>

Now the values are accessible by both opening a named configuration section:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");

var kvpClientID = appSettings.Settings["clientID"];
var kvpClientSecret = appSettings.Settings["clientSecret"];
var kvpCenantID = appSettings.Settings["tenantID"];

string clientID = kvpClientID.Value;

and directly, using ConfigurationManager.AppSettings - a NameValueCollection - which returns the value of the specified key:

string clientID = ConfigurationManager.AppSettings["clientID"];
string clientSecret = ConfigurationManager.AppSettings["clientSecret"];
string tenantID = ConfigurationManager.AppSettings["tenantID"];

As a note, using the file attribute, your ShareAppSettings.debug.config doesn't need (but it's not forbidden) the XML header, it can be just:

<appSettings>
  <add key="clientID" value="11" />
  <add key="clientSecret" value="11" />
  <add key="tenantID" value="11" />
</appSettings>

Secondary note:
you can set the file attribute to point to another file at run-time and refresh the appSettings values to updated the configuration.
Note that, if a file attribute was already set, all values contained in that .config file are not dismissed, but instead moved to the [Application].exe.config file, to become part of the <appSettings> section (thus, they are preserved).
Another reason why using the file attribute may be preferable.

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var appSettings = config.AppSettings;
appSettings.File = "SomeOtherFile.config";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
Jimi
  • 29,621
  • 8
  • 43
  • 61