c# - 获取特定级别的目录

标签 c# directory subdirectory

我想通过动态路径和用户名获取原始文件夹名称,并且没有Windows默认目录

例如,

 C:\Users\dynamic user\Desktop\History\2014-11-03\Spreadsheets\excel.xls
 C:\Users\dynamic user\Desktop\History\record.xls

在这种情况下,我希望它返回 excel.xls 和 record.xls 的“历史记录”。我尝试使用 GetFilename() 方法。但它只是返回

 Spreadsheets - excel.xls
 History      - record.xls

有可能实现吗?感谢您的帮助。

最佳答案

是的,如documented of Path.GetFileName ;

Returns the file name and extension of the specified path string.

一个解决方案是删除您的 Desktop 路径并用 Path.DirectorySeparatorChar 拆分字符并获取第二个元素。

例如;

string path = @"C:\Users\dynamic user\Desktop\History\2014-11-03\Spreadsheets\excel.xls";
var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

path = path.Replace(desktop, "");
var history = path.Split(Path.DirectorySeparatorChar)[1];
Console.WriteLine(history); // History

记住,Environment.SpecialFolder.Desktop返回当前用户桌面信息。

关于c# - 获取特定级别的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26709568/

相关文章:

zend-framework - 在子目录主机上部署模块化 zend 项目

c# - 是否可以使 IEnumerable<char> 实现 IComparable?

C# 或 .NET 刷新键盘缓冲区

c# - LootTable概率分布的简化

python : getcwd and pwd if directory is a symbolic link give different results

c# - 将安全组添加到文件夹问题/System.Security.Principal.IdentityNotMappedException :

c# - 从 C# 调用 C++ 函数时出现访问冲突异常

directory - 生成目录内容的文本表示

apache - CodeIgniter htaccess 子文件夹问题

c# - 如何使用 C# 在 Roaming 中找到文件夹?