Link to home
Start Free TrialLog in
Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates

asked on

Checking File Read and Write Permission

HI,

In .Net 2.0 how do we check that the file has the read and write permissions on it

Thanks
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi tia_kamakshi;

See the sample code below.

Fernando

using System.IO;
 
// Replace the file name in the next line with the file you want info on
FileAttributes fi = File.GetAttributes(@"C:\Temp\myTestData.txt");
if (fi >= 0 )
{
    if ((fi & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
    {
        Console.WriteLine("File is read only");
    }
    else
    {
        Console.WriteLine("File is read write");
    }
}

Open in new window

Here is a link to the other attributes that can be tested for.
http://msdn.microsoft.com/en-us/library/system.io.fileattributes.aspx
Avatar of tia_kamakshi

ASKER

Thanks for your help.

What does that mean
if (fi >= 0)

Is this mean file does not exists or this has some other mean

Thanks again
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many Thanks
Not a problem, glad to help.  ;=)