1

some of my web applications write to the disk on the web server. The paths change depending on location, prod and dev, etc. I used to store the paths in web.config under configuration / appSettings like:

<add key='PDFOutPutPath'   value='C:\Temporary_Web_Files\PDFTempDocs\'/>

And then get them like this:

path = ConfigurationSettings.AppSettings('PDFOutPutPath')

Now in .Net 4, I get compile warnings about this being depreciated, so I found some instructions telling me to add a configuration file, move my values to it like so:

<configuration> 
 <appSetings> 
    <add key='PDFOutPutPath'   value='C:\Temporary_Web_Files\PDFTempDocs\'/>
 </appSettings>
</configuration> 

and use configuration manager like so:

ConfigurationManager.AppSettings( 'PDFOutPutPath' )

However, this does not work. I'm not sure if I'm supposed to be using the configuration manager for this or not - If not, where do you put stuff like this? I have System.Configuration referenced, so this in not my issue.

Brian
  • 548
  • 2
  • 8
  • 22
  • Your use and syntax seems correct (this is VB.Net right?). Can you post the exception you are getting? – EkoostikMartin Jun 20 '12 at 18:48
  • I'm not gertting an exception - the string variable I'm assigning it ( path ) is null. I didn't think the language needed to be specified, but I added a tag. It is a lot like VB though. – Brian Jun 20 '12 at 18:53
  • Wow, a .Net compiler for RPG, never heard of that... Anyhow, can you post the entire contents of your web.config file? – EkoostikMartin Jun 20 '12 at 19:00
  • People always say that! It is pretty slick. However, as stated, I added an application configuration file and put everything in there. Do you want that? – Brian Jun 20 '12 at 19:06
  • Oh, so your app already had a "web.config" file, but you added a second file "app.config" (as per the instructions you followed)? – EkoostikMartin Jun 20 '12 at 19:07

1 Answers1

0

So it appears the instructions you followed were a bit misleading.

You do not need a second file, you should delete the app.config file. You can place all of the configuration values in the web.config. Just make sure the config items are in the <appSetings> node. But you should continue to use the ConfigurationManager class within the code to access the values.

EkoostikMartin
  • 6,831
  • 2
  • 33
  • 62
  • Well.... The reason I did this in the first place is because the file path is needed in a class in the business layer - which is referenced on a web page, but does not have a web.config file. – Brian Jun 20 '12 at 19:28
  • That doesn't matter, the web application will only read from a web.config. It will ignore the app.config. – EkoostikMartin Jun 20 '12 at 19:34
  • Sorry - but I'm confused. You are saying the dll needs to go to the web sites web.config to get a value for a path? What happens if someone else creates a web page and references my dll, but does not include the app key in web.config? Shouldn't the dll be able to get it on it's own? I'm no .Net master - so excuse my ignorance here :P – Brian Jun 20 '12 at 19:41
  • The dll (assembly) is being executed in the context of the website, the website uses only one configuration file "web.config". There is no config file for a dll itself really. If someone else wanted to use your dll assembly inside their own separate website, yes, they would need to include those app keys in the web.config of the site. – EkoostikMartin Jun 20 '12 at 19:47
  • I'll put it back then post the warning message I was getting about being depreciated - most likely not until tomorrow. – Brian Jun 20 '12 at 19:50
  • If you use the `ConfigurationManager` class, you won't get a message about being deprecated. The deprecated part was referring to your use of `ConfigurationSettings` – EkoostikMartin Jun 20 '12 at 19:52
  • I realized this at dinner last night - so you are correct. Although, I'm going to look for another way to do this - maybe using a class as I don't want my business logic to depend on a web.config entry. Marking as answer to give you credit. – Brian Jun 21 '12 at 12:57
  • @Brian, there is one thing you can do, and that is link your main config file "web.config" in this case, to other config files. See this article - http://weblogs.asp.net/fmarguerie/archive/2007/04/26/using-configsource-to-split-configuration-files.aspx – EkoostikMartin Jun 21 '12 at 13:15
  • @ EkoostikMartin Just saw this, and will invistigate. Trying to figure out how to be alertrd to new answers and comments – Brian Jun 26 '12 at 19:57