Re: Check for multiple file attributes

From: Nicholas Paldino [.NET/C# MVP] (mvp_at_spam.guard.caspershouse.com)
Date: 12/22/04


Date: Wed, 22 Dec 2004 14:56:53 -0500

Pete,

    || (and &&) are logical operators, meant to be used on boolean types. |
(and &) are bitwise operators, meant to be used on integral types for bit
masking.

-- 
               - Nicholas Paldino [.NET/C# MVP]
               - mvp@spam.guard.caspershouse.com
"Pete Kane" <pk_fox@ntlworld.com> wrote in message 
news:bUjyd.1640$0k6.204@newsfe1-win.ntli.net...
> sorry Nicholas I posted directly to you, I thought I was on the right 
> lines but it's nice to have confirmation, BTW what's the difference 
> between | and || ?
>
> regards
>
> Pete Kane
>
> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote 
> in message news:%23IJyuJF6EHA.2572@tk2msftngp13.phx.gbl...
>> Pete,
>>
>>    Yes, the FileAttributes enumeration is a bitwise mask, so you would 
>> compare in that manner.  However, you do have to make sure the result is 
>> not zero, because C# does not assume that a non zero integer value 
>> evaluates to true, rather, you have to use a boolean value.
>>
>>    In essence, you need to change your expression to this:
>>
>> if ((File.GetAttributes(fname) & (FileAttributes.ReadOnly | 
>> FileAttributes.Archive)) != 0)
>>
>>    Hope this helps.
>>
>>
>> -- 
>>               - Nicholas Paldino [.NET/C# MVP]
>>               - mvp@spam.guard.caspershouse.com
>>
>>
>> "Pete Kane" <pk_fox@ntlworld.com> wrote in message 
>> news:Fniyd.1525$0k6.840@newsfe1-win.ntli.net...
>>> Hello all, how can I check for multiple file attributes  using bitwise 
>>> operators ? I'm thinking along the lines of
>>>
>>>
>>> string fname = "c:\\test.txt";
>>> if(File.GetAttributes(fname) & (FileAttributes.ReadOnly | 
>>> FileAttributes.Archive ))
>>>
>>> {
>>>
>>> File.SetAttributes(fname,FileAttributes.Normal);
>>>
>>> }
>>>
>>> Basically I'm trying to say "If Readonly or Archive" then set the 
>>> attribute to normal, as you can tell I'm new to C#
>>>
>>>
>>>
>>
>>
>
> 

Quantcast