file path using C#

99封情书 提交于 2019-12-11 07:25:35

问题


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 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.


回答1:


http://msdn.microsoft.com/en-us/library/aa457089.aspx

string path;
path = System.IO.Path.GetDirectoryName( 
  System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/6306992/file-path-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!