Reputation: 727
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.
Upvotes: 43
Views: 54252
Reputation: 11426
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);
}
Upvotes: 0
Reputation: 1963
In VB:
Dim MyNewPath As String = StrReverse(Strings.Split(StrReverse(MyPath), "\", 2)(1))
This works down to the root, eg C:\MyPath
but fails (without validation) for a bare folder.
Obviously, you need to handle differently if there is a file appended.
Upvotes: -1
Reputation: 702
System.IO.DirectoryInfo 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
Upvotes: 7
Reputation: 26267
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
Upvotes: 52
Reputation: 7591
This should account for the path being either a file or directory
DirectoryInfo parent = null;
if (File.Exists(path))
{
parent = new FileInfo(path).Directory.Directory
}
if(Directory.Exists(path))
{
parent = new DirectoryInfo(path).Directory;
}
Upvotes: 2
Reputation: 236208
Thats ugly, but works
string path = @"C:\Documents\MasterDocumentFolder\file.any";
var lastFolder = Path.GetDirectoryName(path);
var pathWithoutLastFolder = Path.GetDirectoryName(lastFolder);
But if you have less than one level of directories (drive root), then pathWithoutLastFolder
will be null
, so you have to deal with it.
Upvotes: 13
Reputation: 3162
Have you tried splitting the string per "\", and then reconstructing a new path by joining every element but the last one?
You would also need to consider the case where the original path is at the root, and when it ends in a backslash or not.
Upvotes: 1