0

We are doing it, I think, 4 different ways. What is the official correct approach we are supposed to use?

  1. Some of our code is a commercial library that is used in web apps, Forms, apps, Silverlight, and more. So the method we use in it must work for all those cases.
  2. We are .NET 3.5 (because as a commercial library we have to run on .NET 2.0).

??? - thanks - dave

David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • I use a different method for Web and Desktop apps. ConfigurationManager.AppSettings["key"] ?? defaultvalue; is what I use. WebConfigurationManger I think is the web one, unless my memory doesn’t serve me. – kenny Jun 21 '12 at 22:32
  • The problem is our library might be part of a web app, or part of a Forms app. So we need a single approach. – David Thielen Jun 21 '12 at 22:35
  • Can you determine which one by looking at what the executing assembly is? Also here's a related SO post. http://stackoverflow.com/questions/272821/get-executing-assembly-name-from-referenced-dll-in-c-sharp – kenny Jun 21 '12 at 22:52
  • Would this technique work for you? http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/17cff6fc-e543-42c3-9d9f-d02922dc1b12 – kenny Jun 21 '12 at 22:55
  • @kenny - The get the executing assembly does not strike me as a safe approach. The link to the technique you posted will I think work. But my question is - what is the way that Microsoft is presently recommending for this. Any idea if that is what they are presently recommending? – David Thielen Jun 21 '12 at 22:57
  • No, but they'll change it soon after ;) sorry. – kenny Jun 21 '12 at 23:09

1 Answers1

0

From msdn:

Using WebConfigurationManager is the preferred way to work with configuration files related to Web applications. For client applications, use the ConfigurationManager class.

Dan Carlstedt
  • 311
  • 2
  • 9
  • Is there a way we can determine which is appropriate? We're a library that gets used in both so we don't know which situation we are in. – David Thielen Jun 21 '12 at 22:44
  • I don't know of a mechanism to determine that. Assuming that you only require read only access to the config you would be safe to use ConfigurationManager and define a custom section for your library. This would work from both web and client apps. – Dan Carlstedt Jun 22 '12 at 03:35