问题
I have a 3.5 .NET app which uses a Class Library. For that class I've created a couple of settings which resulted in this app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="MyClass.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<MyClass.Properties.Settings>
<setting name="FPISORescan" serializeAs="String">
<value>3</value>
</setting>
</MyClass.Properties.Settings>
</userSettings>
</configuration>
This file is copied at compile time in the destination folder as MyClass.dll.config.
From MyClass code I can read the FPISORescan value using this piece of code:
int total_rescan = Convert.ToInt32(global::MyClass.Properties.Settings.Default.FPISORescan);
But if I modify the value to 5 for example in the .config file, the read value remains the same(3). To modify the default value I have to change the setting in Visual Studio and recompile the app.
I have a similar project which works fine, and if I modify a value in the .config file, the application reads the new value, but I don't know what is the difference between the projects.
Thank you.
回答1:
From Visual Studio - Application Settings:
Because there is no configuration file model for class libraries, application settings do not apply for Class Library projects. The exception is a Visual Studio Tools for Office DLL project, which can have a configuration file.
I just tried this, and your right, the settings changed in MyClass.dll.config are ignored.
If however you copy these settings into the configuration file of the calling assembly, you will find it works as you are intending.
If you still want to have the configuration settings in a dll.config file, you could roll your own parser, something like this:
private class ConfigurationManager
{
public static NameValueCollection AppSettings;
public static bool LoadConfig()
{
try
{
string ConfigFile = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.Replace("file:///","") + ".config";
if (System.IO.File.Exists(ConfigFile) == false)
return false;
System.Xml.XmlDocument oXml = new System.Xml.XmlDocument();
oXml.Load(ConfigFile);
System.Xml.XmlNodeList oList = oXml.GetElementsByTagName("appSettings");
AppSettings = new NameValueCollection();
foreach (System.Xml.XmlNode oNode in oList)
foreach (System.Xml.XmlNode oKey in oNode.ChildNodes)
AppSettings.Add(oKey.Attributes["key"].Value, oKey.Attributes["value"].Value);
return true;
}
catch (Exception ex)
{
Console.WriteLine("Error reading configuration: {0}", ex.Message);
return false;
}
}
}
Then you can have a config file belonging to your dll that looks something like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="FPISORescan" value="3"/>
</appSettings>
</configuration>
Then, to use the settings, you can do this:
int total_rescan = Convert.ToInt32(ConfigurationManager.AppSettings["FPISORescan"]);
来源:https://stackoverflow.com/questions/28341368/c-net-read-the-default-value-from-config-at-runtime