-3

Possible Duplicate:
HttpContext.Current.Server null

I have three projects in my ASP .NET solution:

  • a class library
  • a web application
  • a windows service

I have a folder in my web application with an XML file. An XMLreader from class library function needs to be populated with this XML file. I have added this:

var reader = XmlReader.Create(HttpContext.Current.Server.MapPath("~/TestDevice/Data.xml"), settings);

When this function is called from we application, It works. When this function of class library is called from windows application, I get null reference at HttpContext.Current.Server because it is not available in windows application. I don't want to give hardcode path although that works for both windows and web application. Can I use some don't function to access files from different projects or any alternate of server.mappath. Please suggest solution

Thanks

Community
  • 1
  • 1
DotnetSparrow
  • 27,428
  • 62
  • 183
  • 316
  • Fourth time the same question asked by the same user in 2 days. – The Evil Greebo Jun 11 '11 at 01:12
  • if this indeed is the fourth time you ask the question, please bother to learn to format code and lists. – Dan Abramov Jun 11 '11 at 01:14
  • 1
    Stop asking the same question and reply to your current ones http://stackoverflow.com/questions/6304532/httpcontext-current-server-null or http://stackoverflow.com/questions/6310344/httpcontext-current-server-mappath-alternative-in-windows-application or http://stackoverflow.com/questions/6306992/file-path-using-c – keyboardP Jun 11 '11 at 01:16

3 Answers3

9

System.AppDomain.CurrentDomain.BaseDirectory should do the trick.

http://msdn.microsoft.com/en-us/library/system.appdomain.basedirectory.aspx

Gets the base directory that the assembly resolver uses to probe for assemblies.

srn
  • 1,205
  • 9
  • 22
2

HttpContext.Current.Server.MapPath alternative in windows application

file path using C#

HttpContext.Current.Server null

Asking the same basic question four different times isn't going to change the fact that you cannot use a Windows Service to access the web application path and the web app isn't going to know where your service is.

Use a configuration file to define where your data will be stored and provide the same info to both - or use some other storage for your data like a database which has a published location.

Community
  • 1
  • 1
The Evil Greebo
  • 7,013
  • 3
  • 28
  • 55
0

The simplest solution is probably to pass the path to the xml file into that function, rather than making the function itself clever enough to figure it out- then your web app could use the HttpContext method, and the Winforms app would use a regular path. There is no equivalent of the MapPath method for WinForms or windows services.

Chris Shain
  • 50,833
  • 6
  • 93
  • 125