[英]Get Project Root Directory Name in MVC 5
我似乎在任何地方都找不到如何获得项目的第一个文件夹名称的答案。
因此,例如,我有一个BaseSite文件夹,在该解决方案文件所在的BaseSite文件夹中,我有很多项目。 我只需要检索“ BaseSite”名称。
我已经找到了AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
各种示例AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
但这给了您完整的路径,而我的文件夹名称被埋在其中。
与以下内容相同: Server.MapPath("~/App_Data/somedata.xml");
请检查。 它将返回基本目录。
var BaseDirectory = new DirectoryInfo(Server.MapPath("~")).Parent.Name;
和
new DirectoryInfo(Server.MapPath("~/App_Data/somedata.xml")).Parent.Name;
我希望这能帮到您。
//this path will return current project folder
string path = AppDomain.CurrentDomain.BaseDirectory;
DirectoryInfo info = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
string infy = info.Parent.FullName; //this will give you the full path of project folder.
如果必须上一层,则添加
info.Parent.Parent.FullName
并且如果您必须向下一级到子目录。 然后使用
DirectoryInfo[] directoryinfo = info.GetDirectories("SearchPattern or your subdirectory name");
string subdirectory = directoryinfo.FirstOrDefault().FullName;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.