我想通过动态路径和用户名获取原始文件夹名称,并且没有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/