난 그냥 조금 이상한 것 같다이 코드동일한 if 블록에서 2 가지 조건을 테스트하는 다른 방법은 무엇입니까?
if ((fsi.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
//...
}
을 보았다. 테스트의 다른 방법 ?? 나는 두 테스트의 중간에 기대 쉽게
감사
비트 운영자의난 그냥 조금 이상한 것 같다이 코드동일한 if 블록에서 2 가지 조건을 테스트하는 다른 방법은 무엇입니까?
if ((fsi.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
//...
}
을 보았다. 테스트의 다른 방법 ?? 나는 두 테스트의 중간에 기대 쉽게
감사
비트 운영자의FileAttribute를 자세히 살펴보면 속성 표시가 플래그 인 Enum입니다.
당신은 여기에서 더 많은 정보를 얻을 것이다 :
http://dotnetstep.blogspot.com/2009/01/flags-attribute-for-enum.html 지금 '&는'비트 연산자 하나입니다.
예
// Get file Info
System.IO.FileInfo info = new System.IO.FileInfo("C:\\TESTTT.txt");
// Get attribute and convert into int for better understanding
int val = (int)info.Attributes;
// In my case it is 33 whoes binary value for 8 bit 00100001.
// now we perform bitwise end with readonly FileAttributes.ReadOly is 1
// 00100001 & 00000001 = 00000001
int isReadOlny = val & (int)System.IO.FileAttributes.ReadOnly;
Console.WriteLine("IsReadOnly : " + isReadOlny.ToString());
// 00100001 & 00010000 = 00000000
int isDirectory = val & (int)System.IO.FileAttributes.Directory;
Console.WriteLine("IsDirectory : " + isDirectory.ToString());
Console.WriteLine(val);
Console.ReadLine();
희망이 당신을 도울.
여기에 언급 된 &
은 비트와 연산자가 논리가 아니며 (&&
)입니다.
논리 AND ('&& ') 대신 비트 AND ('&')를하고 있습니다. http://msdn.microsoft.com/en-us/library/sbf85k1c(v=vs.71).aspx 및 http://msdn.microsoft.com/en-us/library/cc138362.aspx –