Check for multiple file attributes

P

Pete Kane

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#
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
P

Pete Kane

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 said:
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]
- (e-mail address removed)


Pete Kane said:
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#
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Pete Kane said:
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 said:
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]
- (e-mail address removed)


Pete Kane said:
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#
 
P

Pete Kane

thanks Nicholas, have a good Xmas...

regards

Pete Kane


Nicholas Paldino said:
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]
- (e-mail address removed)

Pete Kane said:
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 said:
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]
- (e-mail address removed)


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#
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top