从完整文件名路径获取文件夹名称

string path = "C:\folder1\folder2\file.txt";

我可以使用哪些对象或方法来得到结果 folder2

344873 次浏览

我可能会使用这样的东西:

string path = "C:/folder1/folder2/file.txt";
string lastFolderName = Path.GetFileName( Path.GetDirectoryName( path ) );

GetDirectoryName的内部调用将返回完整路径,而对GetFileName()的外部调用将返回最后一个路径组件——这将是文件夹名。

无论路径是否实际存在,这种方法都有效。然而,这种方法依赖于最初以文件名结尾的路径。如果不知道路径是否以文件名或文件夹名结束,那么它要求您首先检查实际路径,以查看该位置是否存在文件/文件夹。在这种情况下,丹·迪米特鲁的回答可能更合适。

当路径中没有文件名时,我使用下面的代码片段获取路径的目录:

例如“c:\tmp\test\visual”;

string dir = @"c:\tmp\test\visual";
Console.WriteLine(dir.Replace(Path.GetDirectoryName(dir) + Path.DirectorySeparatorChar, ""));

输出:

视觉

试试这个:

string filename = @"C:/folder1/folder2/file.txt";
string FolderName = new DirectoryInfo(System.IO.Path.GetDirectoryName(filename)).Name;

下面的代码只帮助获取文件夹名称



public ObservableCollection items = new ObservableCollection();


try
{
string[] folderPaths = Directory.GetDirectories(stemp);
items.Clear();
foreach (string s in folderPaths)
{
items.Add(new gridItems { foldername = s.Remove(0, s.LastIndexOf('\\') + 1), folderpath = s });


}


}
catch (Exception a)
{


}
public class gridItems
{
public string foldername { get; set; }
public string folderpath { get; set; }
}
var fullPath = @"C:\folder1\folder2\file.txt";
var lastDirectory = Path.GetDirectoryName(fullPath).Split('\\').LastOrDefault();

这很难看,但避免了分配:

private static string GetFolderName(string path)
{
var end = -1;
for (var i = path.Length; --i >= 0;)
{
var ch = path[i];
if (ch == System.IO.Path.DirectorySeparatorChar ||
ch == System.IO.Path.AltDirectorySeparatorChar ||
ch == System.IO.Path.VolumeSeparatorChar)
{
if (end > 0)
{
return path.Substring(i + 1, end - i - 1);
}


end = i;
}
}


if (end > 0)
{
return path.Substring(0, end);
}


return path;
}

简单的,清洁。只使用System.IO.FileSystem -工作起来很有魅力:

string path = "C:/folder1/folder2/file.txt";
string folder = new DirectoryInfo(path).Name;

DirectoryInfo执行剥离目录名的工作

string my_path = @"C:\Windows\System32";
DirectoryInfo dir_info = new DirectoryInfo(my_path);
string directory = dir_info.Name;  // System32

同样重要的是要注意,在循环中获取目录名列表时,DirectoryInfo类只初始化一次,因此只允许第一次调用。为了绕过这个限制,请确保在循环中使用变量来存储任何单个目录的名称。

例如,这个示例代码循环遍历任何父目录中的目录列表,同时将每个找到的目录名称添加到字符串类型的list中:

(c#)

string[] parentDirectory = Directory.GetDirectories("/yourpath");
List<string> directories = new List<string>();


foreach (var directory in parentDirectory)
{
// Notice I've created a DirectoryInfo variable.
DirectoryInfo dirInfo = new DirectoryInfo(directory);


// And likewise a name variable for storing the name.
// If this is not added, only the first directory will
// be captured in the loop; the rest won't.
string name = dirInfo.Name;


// Finally we add the directory name to our defined List.
directories.Add(name);
}

(VB。净)

Dim parentDirectory() As String = Directory.GetDirectories("/yourpath")
Dim directories As New List(Of String)()


For Each directory In parentDirectory


' Notice I've created a DirectoryInfo variable.
Dim dirInfo As New DirectoryInfo(directory)


' And likewise a name variable for storing the name.
' If this is not added, only the first directory will
' be captured in the loop; the rest won't.
Dim name As String = dirInfo.Name


' Finally we add the directory name to our defined List.
directories.Add(name)


Next directory
string Folder = Directory.GetParent(path).Name;

另一种方法是分割路径,使用c# 8.0中引入的索引结构从路径中获取最后第二个元素。

var path = @"C:\folder1\folder2\file.txt";
var folder = path.Split(@"\")[^2]; // 2nd element from the end


Console.WriteLine(folder); // folder2