我想通过测试该代码来获取解决方案启动项目的父目录路径
string parent = System.IO.Directory.GetParent(Server.MapPath("~/"));
我得到我的解决方案的启动项目当前所在的目录。为什么?
最佳答案
我现在不确定为什么会这样。但你可以做到
string parent = new DirectoryInfo(Server.MapPath("~/")).Parent.FullName;
获取父目录路径。
我试图找到为什么 System.IO.Directory.GetParent(Server.MapPath("~/"))
不起作用的答案,如果我发现了什么,我会更新它。
更新
我在另一个 Stackoverflow question 上找到了可能的答案GSerg 说的是谁
I can only assume Directory.GetParent(...) can't assume that C:\parent\child is a directory instead of a file with no file extension. DirectoryInfo can, because you're constructing the object that way.
关于ASP.NET Directory.GetParent 似乎返回实际文件夹路径,而不是父路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8947129/