How to check a file is ReadOnly in ASP .NET and in C# Example/Sample code

How to check a file is ReadOnly in ASP .NET and in C# Example/Sample code

ASP.NET and C# sample code: 



static bool IsFileOpenOrReadOnly(string file)
{
try
{
            //make sure it's not a read only file
            if ((File.GetAttributes(file) & FileAttributes.ReadOnly) != FileAttributes.ReadOnly)
            {
                  //open the file with a FileStream
                  using (FileStream stream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None))
                  {
                        try
                        {
                            stream.ReadByte();
                            return false;
                        }
                        catch (IOException)
                        {
                            return true;
                        }
                        finally
                        {
                            stream.Close();
                            stream.Dispose();
                        }
                    }
}
else
return true;
}
catch (IOException ex)
{
return true;
}
}

No comments:

Post a Comment