Search code examples
asp.netdirectory-structure

ASP.NET Directory.GetParent seems to be returning the actual folder path, not the parent path


I want to get the parent directory path of my solution's startup project, by testing that code

string parent = System.IO.Directory.GetParent(Server.MapPath("~/"));

I get the directory where my solution's startup project is currently placed. Why ?


Solution

  • I am not sure why this happens, at the momemt. But you can do

    string parent = new DirectoryInfo(Server.MapPath("~/")).Parent.FullName;
    

    to get the parent directory path.

    I try to find a answer why System.IO.Directory.GetParent(Server.MapPath("~/")) does not work and update this if i found something.

    Update

    I found a possible answer on another Stackoverflow question who GSerg say

    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.