c# - 如何从目录符号链接中获取文件
问题描述
我尝试从目录符号链接中获取路径列表。这是写异常
找不到路径的一部分。
var filePath = @"C:\symlink";
var paths = new List<string>((Directory
.GetFiles(filePath, "*.*", SearchOption.AllDirectories))
.OrderBy(x => new FileInfo(x).Name));
解决方案
您必须检查目录是否存在;例如,如果我们想在目录不存在时获得一个空列表:
var filePath = @"C:\symlink";
var paths = Directory.Exists(filePath)
? Directory
.EnumerateFiles(filePath, "*.*", SearchOption.AllDirectories)
.OrderBy(file => Path.GetFileName(file))
.ToList()
: new List<string>();
请注意,我们不必使用which 将所有文件GetFiles(...)
读取到一个数组中,而是可以使用枚举文件EnumerateFiles
推荐阅读
- python - 如何在 Python 中连接到 FTP 服务器?
- html - 制作表格单元格(不是 3x3、2x4!)正方形?
- c# - 说出语音命令时保持玩家移动
- vue.js - vuetify断点未定义,TypeError:无法读取未定义的属性“xsOnly”
- javascript - React 检测到 Hooks 的调用顺序发生了变化,但我看不到任何有条件调用的 hooks
- python - 在块样式 yaml 文件 (ruamel.yaml) 中将来自 Python 的列表显示为流样式
- c# - 如何避免 MailKit 更改 text/plain 和 text/html 部分的传输编码?
- c++ - 从存储的类引用中调用方法
- hibernate - 使用继承重构 Grails 域类
- python - 用于正确并排放置按钮的 tkinter 布局设置的说明