我有一个 Windows 服务,它使用类库中的方法和相同的 asp.net 解决方案。在类库中,我有一个包含以下行的方法:
reader = XmlReader.Create(HttpContext.Current.Server.MapPath("~/TestDevice/Data.xml"), settings);
当控制来到这一行时。我得到异常(exception)。我尝试调试代码,发现当服务尝试访问此方法时,HttpContext.Current.Server 为空。什么是替代语法。
我尝试从 Web 应用程序访问这个类库方法,它工作正常。
System.IO.Path.GetFullPath("/TestDevice/Data.xml")
返回 C:\\TestDevice\\Data.xml
而不是实际目录路径
我想获取文件夹的完整路径。
请提出解决方案。
最佳答案
http://msdn.microsoft.com/en-us/library/aa457089.aspx
string path;
path = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
关于c# - 使用 C# 的文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6306992/