Path problems with ASP.NET Web Application in C#

J

John Hite

I'm writing an ASP.NET Web application in C# that has the following
directory structure:

MyApp\bin\MyApp.dll
MyApp\conf\config.xml
MyApp\webapp.aspx
MyApp\webapp.aspx.cs

ok. The MyApp.dll was created by Visual Studio, webapp.aspx is my
webapp, and webapp.aspx.cs is my codebehind page. I would like to be
able to load and parse the config.xml file when the application
starts. Here is the code I use to open the config file.

XmlTextReader xmlReader= new
XmlTextReader(@"C:\WebApp\conf\config.xml");

Now...This works fine, but I would like to make it so that I don't
have a hard coded path to the config file. I would like to be able to
just say

XmlTextReader xmlReader= new XmlTextReader(@"conf\config.xml");

for example and be able to read the file, but this doesn't work.
whenever I do this I get an error message:

Could not find file "C:\WINDOWS\system32\conf\config.xml".


Any suggestions?

Thanks
 
R

Roger Helliwell

XmlTextReader xmlReader= new
XmlTextReader(@"C:\WebApp\conf\config.xml");

Now...This works fine, but I would like to make it so that I don't
have a hard coded path to the config file. I would like to be able to
just say

XmlTextReader xmlReader= new XmlTextReader(@"conf\config.xml");

for example and be able to read the file, but this doesn't work.
whenever I do this I get an error message:

Could not find file "C:\WINDOWS\system32\conf\config.xml".


Any suggestions?

Thanks

Check out the methods in the Server class. The following might be what
you want:

string sPath = HttpContext.Current.Server.MapPath(@"conf\config.xml");
XmlTextReader xmlReader= new XmlTextReader(sPath);

Roger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top