[英]C#: Need to remove last folder from file name path
我从数据库中提取文件路径以用作文件源。 我需要从源路径中删除最后一个文件夹,然后我可以创建新文件夹以用作目标路径。
示例源文件路径: \\\\\\\\ServerName\\\\Documents\\\\MasterDocumentFolder\\\\
我需要从该字符串中删除最后一个文件夹并获取此\\\\\\\\ServerName\\\\Documents\\\\
: \\\\\\\\ServerName\\\\Documents\\\\
所以我可以创建一个这样的文件夹: \\\\\\\\ServerName\\\\Documents\\\\NewDocumentFolder1\\\\
编辑:我已更新我的示例路径以显示为什么Path.GetDirectoryName()在这种情况下不起作用。
您正在寻找的是Directory类中的GetParent()方法
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
多数民众赞成丑陋,但有效
string path = @"C:\Documents\MasterDocumentFolder\file.any";
var lastFolder = Path.GetDirectoryName(path);
var pathWithoutLastFolder = Path.GetDirectoryName(lastFolder);
但是如果你有不到一个级别的目录(驱动器根目录),那么pathWithoutLastFolder
将为null
,所以你必须处理它。
这应该考虑路径是文件还是目录
DirectoryInfo parent = null;
if (File.Exists(path))
{
parent = new FileInfo(path).Directory.Directory
}
if(Directory.Exists(path))
{
parent = new DirectoryInfo(path).Directory;
}
System.IO.DirectoryInfo可能是完成您所要求的最干净的方法。
var path = "\\\\ServerName\\Documents\\MasterDocumentFolder\\";
string newPath = new DirectoryInfo(path).Parent.CreateSubdirectory("NewDocumentFolder1").FullName;
Console.WriteLine(newPath.FullName);
//> "\\ServerName\Documents\NewDocumentFolder1\"
请注意,DirectoryInfo不需要现有或可访问的目录:
var dir = new DirectoryInfo(@"C:\Asdf\Qwer\Zxcv\Poiu\Lkj\Mn");
Console.WriteLine( dir.Exists );
//> False
但确保它存在是一件轻而易举的事
var dir = new DirectoryInfo(@"C:\Asdf\Qwer\Zxcv\Poiu\Lkj\Mn");
dir.Create();
Console.WriteLine( dir.Exists );
//> True
它还会做一些漂亮的事情,比如解析相对路径:
var dir = new DirectoryInfo(@"C:\Asdf\Qwer\Zxcv\Poiu\Lkj\..\..\..\Mn");
Console.WriteLine( dir.FullName );
//> C:\Asdf\Qwer\Mn
关于修剪和附加斜线的其他答案,请注意Directory.GetParent(“... \\”)和DirectoryInfo(“... \\”)之间的行为差异。处理尾随时的父级 - DirectoryInfo更一致:
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
同样,为避免处理尾部斜杠,请始终使用Path.Combine()来连接路径和文件名。 无论它们是否包含尾随符号,它都将正确处理路径:
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
您是否尝试按“\\”分割字符串,然后通过连接除最后一个元素之外的每个元素来重构新路径?
您还需要考虑原始路径位于根路径的情况,以及何时以反斜杠结束的情况。
使用此方法,您可以通过dirPath创建dir(如果dir不存在),并根据需要从filePath创建目录
private void CreateDirIfNotExist(string dirPath, bool removeFilename = false)
{
if (removeFilename)
dirPath = Directory.GetParent(dirPath).FullName;
if (!Directory.Exists(dirPath))
Directory.CreateDirectory(dirPath);
}
在VB中:
Dim MyNewPath As String = StrReverse(Strings.Split(StrReverse(MyPath), "\", 2)(1))
这适用于根,例如C:\\MyPath
但是对于裸文件夹失败(没有验证)。
显然,如果附加了文件,则需要以不同的方式处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.