0

何らかの理由で、FileAttributes.Normalに対してファイルをチェックしようとすると、検索にほとんどファイルが表示されていないようです。APIによると、これは他の属性が設定されているためです。それは大丈夫です、私は私が望まないもので検索する必要があります。ここで問題が発生します。

特に一度に複数のことをチェックすることになると、ビット単位の演算子の計算に非常に錆びています。特定の数のファイル属性のいずれかが存在する場合にfalseを返すifステートメントを取得する方法を理解しようとしています(つまり、検索を通過したい場合、指定された属性のどれも見つかりません)。これが私がこれまでに書いたものです:

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

私は、すべての異なる属性の集合体を取り、それらをファイルの元の属性と比較する必要があると考えています。一致するものが見つかった場合、全体がゼロになります。ただし、これは意図したとおりに機能していないようです。ビット単位および(&)とは異なる演算子を使用する必要がありますか?

ありがとう!

アップデート:

問題となったのはビットマスクロジックではなく、FileAttributes.Archiveのようです。何らかの理由で、私のファイルはほぼすべてこのフラグでマークされていました(おそらくバックアップ用であることを示していますか?)。少なくとも今、私は知っています、そして知ることは戦いの半分です。:)

4

3 に答える 3

1

これが行っているのは、選択した属性だけを提供することです。

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

そこでは、平等を不平等と交換するだけです。このように、これらの属性のいずれかが存在する場合、最終結果はゼロ以外になります。

于 2012-06-19T22:33:02.980 に答える
1

Normal属性のみが設定されたファイルが必要な場合は、

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

MSDNによるとFileAttributes.NormalはThe file is normal and has no other attributes set. This attribute is valid only if used alone.

いくつかの調査の後、問題はアーカイブ属性にあると思います。この単純なコードでテストしますか?

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 に答える
0

あなたの論理は逆です。一致するものが見つかった場合、ビット単位のand(&)の結果はゼロ以外です。

例:

一致: 0101000&0100000 = 0100000!= 0

一致しない: 0101000&0010000 = 0000000

于 2012-06-19T22:30:23.323 に答える