开发者

How to use app settings with class libraries on winforms?

If I create a app.config for my main app, I can use it using:

string test = Properties.Settings.Default.LibTest;

Where on the app.config you can find:

<开发者_Go百科applicationSettings>
<RManager.Properties.Settings>
  <setting name="LibTest" serializeAs="String">
    <value>ola lib</value>
  </setting>
</RManager.Properties.Settings>

The question is, if I have a class library, is there a way to access the same setting using:

string libString = Properties.Settings.Default.LibTest;

If not, which solution is the best to access the settings from the main app?


I'd say that it's not correct if your libruary can get property from the main app. I'd suggest folowing:

Create another static class (f.e. SettingsManager) in libruary (or in some common lib), and after application started fill the data from the appconfig to that common static class (SettingsManager). And that class(SettingsManager) property you can use in your app and in your dll.

If your dll and your app has a lot of bindings, than you should consider taking the source from the dll to exe app, in that case you would be able to access app.config settings.


Essentially you need to make the settings public and access them from another assembly.

Here's my answers to a couple of previous question that are similar in nature:

  • C# Modify Connection String that exists inside a library
  • Open other program's configuration files
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜