Question: I'm pulling a file path from a database to use as a file source.
I need to remove the last folder from the source path, so I can then create new folders to use as the destination path.

Example source file path:
`\\\\ServerName\\Documents\\MasterDocumentFolder\\`

I need to remove the last folder from that string and get this:
`\\\\ServerName\\Documents\\`

So I can create a folder like this:
`\\\\ServerName\\Documents\\NewDocumentFolder1\\`

Edit: I have updated my example paths to show why the Path.GetDirectoryName() won't work in this case.

Answer: 
What you are looking for is the GetParent() method in the Directory class

```
string path = @"C:\Documents\MasterDocumentFolder\";
DirectoryInfo parentDir = Directory.GetParent(path);
// or possibly
DirectoryInfo parentDir = Directory.GetParent(path.EndsWith("\\") ? path : string.Concat(path, "\\"));

// The result is available here
var myParentDir = parentDir.Parent.FullName

```
With this method you can create dir by dirPath (if dir is not exist) and to create directory from the filePath if needed

```
private void CreateDirIfNotExist(string dirPath, bool removeFilename = false)
{
    if (removeFilename)
        dirPath = Directory.GetParent(dirPath).FullName;

    if (!Directory.Exists(dirPath))
        Directory.CreateDirectory(dirPath);
}

```