server.mappath null [duplicate]
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 solu开发者_运维知识库tion
Thanks
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.
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.
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.
精彩评论