0

出于某种原因,当我尝试根据 FileAttributes.Normal 检查文件时,似乎我的文件几乎都没有出现在我的搜索中。根据 API,这是因为设置了一些其他属性。没关系,我只需要搜索我想要的东西。这就是问题所在。

我对我的位运算符数学非常生疏,尤其是在一次检查多个事物时。我试图弄清楚如果存在一定数量的文件属性中的任何一个(即如果要通过我的搜索,则找不到指定的属性),如何让 if 语句返回 false。这是我到目前为止所写的:

if ((File.GetAttributes(stringFileName) &
   (FileAttributes.System | FileAttributes.Hidden | FileAttributes.Archive |
    FileAttributes.Encrypted | FileAttributes.Temporary)) == 0)

我认为它应该综合所有不同的属性并将它们与文件的原始属性进行比较。如果找到任何匹配项,则将整个事物呈现为零。但是,这似乎没有按预期工作。我应该使用与按位和 (&) 不同的运算符吗?

谢谢!

更新:

看来问题不是位掩码逻辑,而是 FileAttributes.Archive。由于某种原因,我的文件几乎都标有这个标志(也许表明它们注定要备份?)。至少现在我知道了,知道是成功的一半。:)

4

3 回答 3

1

If you want the files with only the Normal attribute set then

if (File.GetAttributes(stringFileName) == FileAttributes.Normal)
    // True, file with only Normal attribute
else
    // False, file with some attributes but not the Normal one

According to MSDN FileAttributes.Normal means The file is normal and has no other attributes set. This attribute is valid only if used alone.

After some research I think that the problem lies in the Archive attribute. Would you like to test with this simple code?

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Usage: CheckAllAtt <directoryName>");
                return;
            }
            var files = Directory.GetFiles(args[0]);
            foreach (string fileName in files)
            {
                FileAttributes att = File.GetAttributes(fileName);
                DumpAttr(fileName, att);
            }
        }
        private static void DumpAttr(string fileName, FileAttributes att)
        {
            StringBuilder sb = new StringBuilder("File: " + fileName);
            if ((att & FileAttributes.Archive) == FileAttributes.Archive)
                sb.Append(" Archive,");
            if ((att & FileAttributes.Compressed) == FileAttributes.Compressed)
                sb.Append(" Compressed,");
            if ((att & FileAttributes.Device) == FileAttributes.Device)
                sb.Append(" Device,");
            if ((att & FileAttributes.Directory) == FileAttributes.Directory)
                sb.Append(" Directory,");
            if ((att & FileAttributes.Encrypted) == FileAttributes.Encrypted)
                sb.Append(" Encrypted,");
            if ((att & FileAttributes.Hidden) == FileAttributes.Hidden)
                sb.Append(" Hidden,");
            if ((att & FileAttributes.Normal) == FileAttributes.Normal)
                sb.Append(" Normal,");
            if ((att & FileAttributes.NotContentIndexed) == FileAttributes.NotContentIndexed)
                sb.Append(" Normal,");
            if ((att & FileAttributes.Offline) == FileAttributes.Offline)
                sb.Append(" Offline,");
            if ((att & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                sb.Append(" ReadOnly,");
            if ((att & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
                sb.Append(" ReparsePoint,");
            if ((att & FileAttributes.SparseFile) == FileAttributes.SparseFile)
                sb.Append(" SparseFile,");
            if ((att & FileAttributes.System) == FileAttributes.System)
                sb.Append(" System,");
            if ((att & FileAttributes.Temporary) == FileAttributes.Temporary)
                sb.Append(" Temporary,");

            sb.Length -= 1;
            Console.WriteLine(sb.ToString());
        }
    }
}
于 2012-06-19T22:52:09.080 回答
1

这样做只是为您提供您选择的那些属性。

if ((File.GetAttributes(stringFileName) &
    (FileAttributes.System | FileAttributes.Hidden | FileAttributes.Archive |
    FileAttributes.Encrypted | FileAttributes.Temporary)) != 0)

在那里,只需将等式换成不等式。这样,如果存在任何这些属性,则最终结果将非零。

于 2012-06-19T22:33:02.980 回答
0

你的逻辑是倒退的。如果找到任何匹配项,则按位和 (&) 的结果非零。

例子:

匹配: 0101000 & 0100000 = 0100000 != 0

不匹配: 0101000 & 0010000 = 0000000

于 2012-06-19T22:30:23.323 回答