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

```
[System.IO.DirectoryInfo](http://msdn.microsoft.com/en-us/library/system.io.directoryinfo%28v=vs.110%29.aspx) is probably the cleanest way to accomplish what you're asking for.

```
var path = "\\\\ServerName\\Documents\\MasterDocumentFolder\\";
string newPath = new DirectoryInfo(path).Parent.CreateSubdirectory("NewDocumentFolder1").FullName;
Console.WriteLine(newPath.FullName);
//> "\\ServerName\Documents\NewDocumentFolder1\"

```

Note that DirectoryInfo does NOT require an existing or accessible directory:

```
var dir = new DirectoryInfo(@"C:\Asdf\Qwer\Zxcv\Poiu\Lkj\Mn");
Console.WriteLine( dir.Exists );
//> False

```

But making sure it exists is a snap

```
var dir = new DirectoryInfo(@"C:\Asdf\Qwer\Zxcv\Poiu\Lkj\Mn");
dir.Create();
Console.WriteLine( dir.Exists );
//> True

```

It will also do nifty things like resolve relative paths:

```
var dir = new DirectoryInfo(@"C:\Asdf\Qwer\Zxcv\Poiu\Lkj\..\..\..\Mn");
Console.WriteLine( dir.FullName );
//> C:\Asdf\Qwer\Mn

```

Regarding other answers trimming and appending slashes, note the difference in behavior between Directory.GetParent("...\") and DirectoryInfo("...\").Parent when dealing with trailing \'s - DirectoryInfo is more consistent:

```
Console.WriteLine( Directory.GetParent( @"C:\Temp\Test" ).FullName );
//> C:\Temp
Console.WriteLine( Directory.GetParent( @"C:\Temp\Test\" ).FullName );
//> C:\Temp\Test
Console.WriteLine( new DirectoryInfo( @"C:\Temp\Test" ).Parent.FullName );
//> C:\Temp
Console.WriteLine( new DirectoryInfo( @"C:\Temp\Test\" ).Parent.FullName );
//> C:\Temp

```

Again, to avoid dealing with trailing slashes, always use Path.Combine() to concatenate paths and file names. It will handle paths correctly whether they contain a trailing \ or not:

```
Console.WriteLine( Path.Combine( @"C:\Temp\Test\", "Test.txt" ) );
//> C:\Temp\Test\Test.txt
Console.WriteLine( Path.Combine( @"C:\Temp\Test", "Test.txt" ) );
//> C:\Temp\Test\Test.txt
Console.WriteLine( Path.Combine( @"C:\", "Temp", "Test", "Test.txt" ) );
//> C:\Temp\Test\Test.txt

```