从 Directory.GetFiles 列表中过滤出临时文件 C#

发布于 2024-12-09 12:30:54 字数 472 浏览 0 评论 0原文

我尝试仅保留文件扩展名为 .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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦上情书 2024-12-16 12:30:54

或者,您可以使用 HashSet 简化逻辑:

readonly HashSet<string> AllowedExtensions = new HashSet<string>(
    { ".SLDPRT", ".SLDASM", ".SLDDRW" },
    StringComparer.OrdinalIgnoreCase);

并在获取文件的方法中:

string[] solidworksFileList = Directory.GetFiles(args[0], "*.SLD???",
    SearchOption.AllDirectories)
    .Where(str => AllowedExtensions.Contains(Path.GetExtension(str)) &&
                  !Path.GetFileName(str).StartsWith("~$"))
    .ToArray();

这将是一个更通用的解决方案,因为您可以向 HashSet 添加任意数量的扩展code> 和查找仍将接近恒定时间。这比输入任意数量的 str.EndsWith 条件要容易得多。

Or you can simplify your logic with a HashSet:

readonly HashSet<string> AllowedExtensions = new HashSet<string>(
    { ".SLDPRT", ".SLDASM", ".SLDDRW" },
    StringComparer.OrdinalIgnoreCase);

and in your method that gets the files:

string[] solidworksFileList = Directory.GetFiles(args[0], "*.SLD???",
    SearchOption.AllDirectories)
    .Where(str => AllowedExtensions.Contains(Path.GetExtension(str)) &&
                  !Path.GetFileName(str).StartsWith("~$"))
    .ToArray();

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 of str.EndsWith conditionals.

So要识趣 2024-12-16 12:30:54

您可以添加另一个位置来忽略以 ~$ 开头的文件

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))
.Where(str => !str.Contains(@"\~$")
.ToArray();

You can add another where to ignore files starting with ~$

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))
.Where(str => !str.Contains(@"\~$")
.ToArray();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文