从 Directory.GetFiles 列表中过滤出临时文件 C#
我尝试仅保留文件扩展名为 .SLDPRT、.SLDASM 和 .SLDDRW 的文件。但是,我不断收到以 ~$ 开头的隐藏临时文件,例如 C:\directory\~$IamAtemporaryfile.sldprt 。您对摆脱这些有什么建议?
string[] solidworksFileList = Directory.GetFiles(args[0], "*.SLD???",
SearchOption.AllDirectories).Where(str => str.EndsWith(".SLDPRT",
StringComparison.OrdinalIgnoreCase) || str.EndsWith(".SLDASM",
StringComparison.OrdinalIgnoreCase) || str.EndsWith(".SLDDRW",
StringComparison.OrdinalIgnoreCase)).ToArray();
I am trying to only keep files with the file extensions .SLDPRT, .SLDASM, and .SLDDRW. However, I keep getting hidden temporary files that begin with ~$ like C:\directory\~$IamAtemporaryfile.sldprt . What are your suggestions to get rid of those?
string[] solidworksFileList = Directory.GetFiles(args[0], "*.SLD???",
SearchOption.AllDirectories).Where(str => str.EndsWith(".SLDPRT",
StringComparison.OrdinalIgnoreCase) || str.EndsWith(".SLDASM",
StringComparison.OrdinalIgnoreCase) || str.EndsWith(".SLDDRW",
StringComparison.OrdinalIgnoreCase)).ToArray();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者,您可以使用
HashSet
简化逻辑:并在获取文件的方法中:
这将是一个更通用的解决方案,因为您可以向
HashSet
添加任意数量的扩展code> 和查找仍将接近恒定时间。这比输入任意数量的str.EndsWith
条件要容易得多。Or you can simplify your logic with a
HashSet
:and in your method that gets the files:
This would be a more general solution in that you could add any number of extensions to the
HashSet
and lookup would still be near constant time. And it's a whole lot easier than typing an arbitrarily large number ofstr.EndsWith
conditionals.您可以添加另一个位置来忽略以 ~$ 开头的文件
You can add another where to ignore files starting with ~$