简体   繁体   中英

Get Project Root Directory Name in MVC 5

I can't seem to find an answer anywhere on how to get the name of the first folder of my project.

So for Example I have a BaseSite folder and inside that BaseSite folder is where my solution file sits, in there I have many projects. I need to retrive the "BaseSite" name and only that.

I have found various examples with AppDomain.CurrentDomain.GetData("DataDirectory").ToString(); but that gives you the full path and my folder name is buried in there.

Same with: Server.MapPath("~/App_Data/somedata.xml");

Please check. It will return the base directory.

var BaseDirectory = new DirectoryInfo(Server.MapPath("~")).Parent.Name;

And

new DirectoryInfo(Server.MapPath("~/App_Data/somedata.xml")).Parent.Name;

I hope this will help you.

    //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.

if you have to go one level up then add

info.Parent.Parent.FullName

And if you have to go one level down to subdirectory. Then use

     DirectoryInfo[] directoryinfo = info.GetDirectories("SearchPattern or your subdirectory name");
    string subdirectory = directoryinfo.FirstOrDefault().FullName;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM