string path = "C:\folder1\folder2\file.txt";
我可以使用哪些对象或方法来给我结果
folder2
?
我可能会使用类似的东西:
string path = "C:/folder1/folder2/file.txt";
string lastFolderName = Path.GetFileName( Path.GetDirectoryName( path ) );
对
GetDirectoryName
的内部调用将返回完整路径,而对 GetFileName()
的外部调用将返回最后一个路径组件 - 这将是文件夹名称。
无论路径是否实际存在,此方法都有效。然而,这种方法确实依赖于最初以文件名结尾的路径。如果未知路径是否以文件名或文件夹名称结尾 - 那么它要求您首先检查实际路径以查看该位置是否存在文件/文件夹。那么,Dan Dimitru 的回答可能更合适。
试试这个:
string filename = @"C:/folder1/folder2/file.txt";
string FolderName = new DirectoryInfo(System.IO.Path.GetDirectoryName(filename)).Name;
简单干净。仅使用
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
当路径中没有文件名时,我使用此代码片段来获取路径的目录:
例如“c: mp est isual”;
string dir = @"c:\tmp\test\visual";
Console.WriteLine(dir.Replace(Path.GetDirectoryName(dir) + Path.DirectorySeparatorChar, ""));
输出:
视觉
string Folder = Directory.GetParent(path).Name;
var fullPath = @"C:\folder1\folder2\file.txt";
var lastDirectory = Path.GetDirectoryName(fullPath).Split('\\').LastOrDefault();
还需要注意的是,在循环中获取目录名称列表时,
DirectoryInfo
类会初始化一次,因此只允许首次调用。为了绕过此限制,请确保在循环中使用变量来存储任何单个目录的名称。
例如,此示例代码循环遍历任何父目录中的目录列表,同时将每个找到的目录名称添加到字符串类型列表中:
[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.NET]
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 path = "C:/folder1/folder2/file.txt";
string folder = new DirectoryInfo(path).Parent.Name;
输出:文件夹2
下面的代码有助于仅获取文件夹名称
公共 ObservableCollection 项目 = new ObservableCollection(); 尝试 { string[]folderPaths = Directory.GetDirectories(stemp); items.Clear(); foreach(文件夹路径中的字符串) { items.Add(new gridItems { 文件夹名称 = s.Remove(0, s.LastIndexOf('\') + 1), 文件夹路径 = s }); } } 捕获(异常a) { } 公共类网格项目 { 公共字符串文件夹名称 { get;放; } 公共字符串文件夹路径{获取;放; } }
另一种方法是分割路径并使用 C# 8.0 中引入的 Index Struct 从路径中获取倒数第二个元素。
var path = @"C:\folder1\folder2\file.txt";
var folder = path.Split(@"\")[^2]; // 2nd element from the end
Console.WriteLine(folder); // folder2
这是我的口味。它的优点是可以处理任何文件路径或目录路径,无论尾随分隔符如何。
public static string GetFolderNameFromPath(string path)
{
// Handle no path
if (string.IsNullOrWhiteSpace(path))
return string.Empty;
// Handle edge case (filename followed by slash)
// Path.GetDirectoryName("C:\\test\\file.text\\") will incorrectly return file.text.
// This is because GetDirectoryName uses split on directory seprator and just gets the last element.
path = path.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
return Path.HasExtension(path) ? new DirectoryInfo(Path.GetDirectoryName(path)).Name : new DirectoryInfo(path).Name;
}
这很丑陋,但避免了分配:
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;
}