[英]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.