使用C#文件路径 [英] file path using C#
问题描述
我有使用方法,由一个类库用同样的asp.net解决方案Windows服务。在类库,我有以下行的方法:
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);
在控制谈到此行。我得到异常。我试着调试code和发现,当服务试图访问此方法,那么HttpContext.Current.Server为null。什么是另类的语法。
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.
我试图来访问Web应用程序这个类库方法,它工作正常。
I tried to access this class library method from web application and it works fine.
System.IO.Path.GetFullPath(/为TestDevice / data.xml中)
收益 C:\\\\ \\\\为TestDevice data.xml中
而不是实际的目录路径
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.
请提出解决方案。
推荐答案
HTTP: //msdn.microsoft.com/en-us/library/aa457089.aspx
string path;
path = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
这篇关于使用C#文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!