1

Basically i have a .net application that has a directory path stored in the app.config file. this directory path outputs xml files that will be read by an asp.net web page.

Is there any way i can get the asp.net web page to read the directory path stored in the app.config file? Should i look to use the web.config file at all?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Simon
  • 133
  • 1
  • 6
  • 15

3 Answers3

2

The easiest thing to do would be to replicate the directory path into the web.config file of your asp.net web application. You could put the path in the AppSettings element of the web.config file as follows:

<appSettings>
  <add key="FilePath" value="d:\fileDirectory" />
</appSettings>

You can then read this value from your asp.net app using the WebConfigurationManager or the ConfigurationManager. The WebConfigurationManager is the preferred method to use since it know how to handle ASP.Net configuration inheritance (see Antonio's comment below).

You will need to insure that the windows account under which the asp.net process is running has read privileges on the specified directory where the XML files are being stored. You can adjust this using the ACL settings of the directory.

Alternatively, instead of replicating the the directory path in web.config, could try to have your asp.net app directly read the path from the app.config file of your .net app. In this case, you would need to load the contents of the file into an XDocument or use the configuration-parsing tools in .net, and then parse the file to extract the value. You would need to make sure your asp.net app has permissions to read the app.config file. And you would still need to store a path in you web.config, this time to point towhees the app.config file is located. so personally, I would just replicate the path of the xml files into the web.config file of the asp.net app.

Joe Alfano
  • 10,149
  • 6
  • 29
  • 40
  • 1
    almost +1 ;) for ASP.NET applications it's prefered to use WebConfigurationManager, since it can handle web.config inheritance http://stackoverflow.com/questions/698157/whats-the-difference-between-the-webconfigurationmanager-and-the-configurationm – Antonio Bakula Feb 19 '12 at 20:55
  • Thanks Antonio for pointing that out. I modified my response post accordingly. – Joe Alfano Feb 19 '12 at 23:27
0

To read a app setting from web.xml use
ConfigurationManager under System.Configuration namespace

<appSettings>
    <add key="filepath" value="D:\folder"/>
</appSettings>

To read this setting

ConfigurationManager.AppSettings["filepath"].ToString()
sunil gautam
  • 451
  • 3
  • 6
-1
Dim Xmldoc As New XmlDocument
Dim xmlatt As XmlAttribute
xmldoc.load("your file path")
xmlatt =xmldoc.SelectSingleNode("/configuration/appSettings/add[@key = 'keyname']/@value")

you can use the values as xmlatt.value

Taryn
  • 242,637
  • 56
  • 362
  • 405