简体   繁体   English

隐藏隐藏的文档,并从C#目录中排除文件扩展名

[英]Hiding hidden documents and excluding file extensions from a directory in C#

I was searching around the internet and found some solutions but they don't seem to work in my case and I don't know why. 我在互联网上搜索并找到了一些解决方案,但对于我来说它们似乎不起作用,我也不知道为什么。 I list all files in the working directory which are .xls files. 我列出了工作目录中所有的.xls文件。 Now since I'm working with an older version of Office I want to exclude the .xlsx files and also all temporary files but this doesn't seem to work. 现在,由于我使用的是Office的较早版本,因此我希望排除.xlsx文件以及所有临时文件,但这似乎不起作用。 I tried: 我试过了:

 FileAttributes fileAttribute = File.GetAttributes(Directory.GetCurrentDirectory());

   string[] filePaths = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.xls")
   .Where(name => !name.EndsWith(".xlsx") || !name.Contains(@"\~$") 
   || (fileAttribute & FileAttributes.Hidden) == 0).ToArray();

Now I tried it without the first line and with " name.Attributes " since " fileAtrribute "but he can't find it. 现在,由于没有“ fileAtrribute”,我没有第一行就使用“ name.Attributes”进行了尝试,但是他找不到它。 I also tried to search for "?.xls" but then he doesn't list any files. 我也尝试搜索“?.xls”,但是他没有列出任何文件。 What am I doing wrong? 我究竟做错了什么?

You have a logic issue with: 您有以下逻辑问题:

string[] filePaths = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.xls")
   .Where(name => !name.EndsWith(".xlsx") || !name.Contains(@"\~$") 
   || (fileAttribute & FileAttributes.Hidden) == 0).ToArray()

you want ands, not ors.. 你要和,而不是

string[] filePaths = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.xls")
   .Where(name => !name.EndsWith(".xlsx") && !name.Contains(@"\~$") 
   && (fileAttribute & FileAttributes.Hidden) == 0).ToArray()

However, your fileattribute is checking the directory, not the file. 但是,您的文件属性正在检查目录,而不是文件。

So you actually want 所以你实际上想要

string[] filePaths = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.xls")
   .Where(name => !name.EndsWith(".xlsx") && !name.Contains(@"\~$") 
   && (File.GetAttributes(name) & FileAttributes.Hidden) == 0).ToArray()

You have a couple of issues. 您有几个问题。 Firstly, your || 首先,您的|| 's should be && 's. 的应该是&& You want to exclude files where the name doesn't end with xlsx AND it doesn't contain ~$ AND where it's not hidden. 您想排除名称不以xlsx结尾并且不包含〜$并且不隐藏的文件。

Secondly, your check for the hidden property looks incorrect. 其次,对隐藏属性的检查看起来不正确。 Currently you are grabbing the attributes of the folder and then comparing that with each file. 当前,您正在获取文件夹的属性,然后将其与每个文件进行比较。 You really want to get the properties of each file. 您确实要获取每个文件的属性。

In summary, you need: 总之,您需要:

string[] filePaths = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.xls")
.Where(name => !name.EndsWith(".xlsx") && !name.Contains(@"\~$")
&& (File.GetAttributes(name) & FileAttributes.Hidden) == 0).ToArray();

由于调用Directory.GetFiles(Directory.GetCurrentDirectory(),“ *。xls”),您已经排除了.xlsx和临时文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM