Click here to Skip to main content
15,891,774 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
hi all,

i need to check whether a directory is read only. if read only is true then i need to remove that. how can i do this using C#.

framework2.0

thanks
nidheesh
Posted

Take a look at the answer to this question[^].
 
Share this answer
 
Comments
nidheeshkayal 31-May-11 13:46pm    
hi..

i tried the following

DirectoryInfo di = new DirectoryInfo(@"e:\\test");
if (di.Attributes == FileAttributes.ReadOnly)
{
MessageBox.Show("Read only");
}
else
{
MessageBox.Show("No");
}

but it returns "No" all the time.
Marc A. Brown 31-May-11 14:03pm    
That's because DirectoryInfo.Attributes can hold multiple attributes via a "bitwise combination of its member values". http://msdn.microsoft.com/en-us/library/system.io.fileattributes(v=vs.71).aspx
yesotaso 31-May-11 15:57pm    
5+ from me. To OP: What is the result of "10 | 4" and "14 | 4"? Answer is "14" and "14". If you ask "14 == 4" answer is false, but if you ask "(14 | 4) == 14" answer is true.
OP says he has tried the following code to test whether a directory is read-only:

C#
DirectoryInfo di = new DirectoryInfo(@"e:\\test");
if (di.Attributes == FileAttributes.ReadOnly)
{
    MessageBox.Show("Read only"); }
else
{
    MessageBox.Show("No");
}


This is almost correct, it just doesn't take into account the fact that the Attributes property is a "flag" enum, meaning that it can be set to any combination of the available FileAttributes[^] values.

In other words, for a folder, the Attributes property will contain at least FileAttributes.Directory, along with any other flags that may be set (like the ReadOnly flag).

If we're only interested in a particular flag (in this case, whether it's read-only), we need to do this:
C#
if ((di.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)


The rest of the code can stay as-is.

To clear the read-only flag from a folder, you need to unset the ReadOnly flag in the Attributes property:

di.Attributes = (di.Attributes ^ FileAttributes.ReadOnly); // This clears just the ReadOnly flag, the rest of the flags stay the same.

Note that this may fail if you don't have the correct permissions to make changes to this folder, or if it's a system folder (i.e. it has the FileAttributes.System flag set).

Hope this helps.
 
Share this answer
 
v2
There is a function HasAttribute
Never used it though but this shall work:
C#
DirectoryInfo di = new DirectoryInfo(@"e:\\test");
if (di.Attributes.HasFlag(FileAttributes.ReadOnly))
{
    //Your Logic Here
}
 
Share this answer
 
Comments
Deepu S Nair 2-Feb-15 23:58pm    
Answering old questions adds nothing to the previous solution and is likely to attract
downvoting.
CHill60 3-Feb-15 5:33am    
I also suggest that you read the question more carefully - OP stated that they were using framework 2.0 - HasFlag was introduced in 4.0.
If you had put more words around the solution (for example explaining that there is now a HasFlag method) you would attract fewer downvotes
 
Share this answer
 
Comments
Deepu S Nair 3-Feb-15 3:10am    
Question was more than 4 years old.Why you are answering it?
CHill60 4-Feb-15 3:51am    
This is the third "answer" from you to old posts. This "reputation hunting" essentially has the opposite effect due to downvotes, but more importantly is considered abuse and can lead to your account being suspended. Please stop doing it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900