2011-12-26 2 views
0

난 그냥 조금 이상한 것 같다이 코드동일한 if 블록에서 2 가지 조건을 테스트하는 다른 방법은 무엇입니까?

if ((fsi.Attributes & FileAttributes.Directory) == FileAttributes.Directory) 
{ 
    //... 
} 

을 보았다. 테스트의 다른 방법 ?? 나는 두 테스트의 중간에 기대 쉽게

감사

비트 운영자의
+1

논리 AND ('&& ') 대신 비트 AND ('&')를하고 있습니다. http://msdn.microsoft.com/en-us/library/sbf85k1c(v=vs.71).aspx 및 http://msdn.microsoft.com/en-us/library/cc138362.aspx –

답변

1

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(); 

희망이 당신을 도울.

1

&

if ((FirstName=="Richard") & (LastName == "DeFortune") 
{ 
    //... 
} 

같은 것을 기대하는 것입니다.

fsi.AttributesFileAttributes.Directory 비트 세트가 있는지 확인합니다.

+0

을 참조하십시오. 미안해, 나는 그것을 얻지 않았다. "할당하지 않으면 먼저 확인"을 의미합니다. ??? – Richard77

+0

아니오; 그렇지 않습니다. http://en.wikipedia.org/wiki/Bitwise_operation – SLaks

0

여기에 언급 된 &은 비트와 연산자가 논리가 아니며 (&&)입니다.

관련 문제