file path using C#
I have a windows service which is using a method from a class library with same asp.net solution. in class library, I have a method with following line:
reader = XmlReader.Create(HttpContext.Current.Server.MapPath("~/TestDevice/Data.xml"), settings);
When control comes to this line. I get exception. I 开发者_JAVA技巧tried to debug the code and found that when service tries to access this method then HttpContext.Current.Server is null. What is alternative syntax.
I tried to access this class library method from web application and it works fine.
System.IO.Path.GetFullPath("/TestDevice/Data.xml")
returns C:\\TestDevice\\Data.xml
instead of the actual directory path
I want to get full path of the folder.
Please suggest solution.
http://msdn.microsoft.com/en-us/library/aa457089.aspx
string path;
path = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
You will need a configuration file that can have the "root" directory set specifically. This will allow the windows service to know what directory to place files into regardless of where its executable sits and regardless of where the asp.net site is configured to run.
I don't think the ~
will work in this case, you will need to provide a relative path. Something like "../../TestDevice/Data.xml"
should work.
精彩评论