gpt4 book ai didi

c# - 使用 C# 的文件路径

转载 作者:太空宇宙 更新时间:2023-11-03 20:33:21 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com