How to check if a file is read-only in C#

 
Following code example shows how to check that file is read only or not in C# language.

private void CheckFileIsReadOnlyOrNot()
    {
      try
         {
             System.IO.FileInfo _file = new System.IO.FileInfo("d:\\report.txt");
             if ((_file.Attributes & System.IO.FileAttributes.ReadOnly) ==
                 System.IO.FileAttributes.ReadOnly)
             {
                 MessageBox.Show("File is read-only");
             }
          }
      catch { }
   }

Author: Ankur

Have worked primarily in the domain of Calling, CRM and direct advertisers services. My technological forte is Microsoft Technologies especially Dot Net (Visual Studio 2003, 2005, 2008, 2010 and 2012) and Microsoft SQL Server 2000,2005 and 2008 R2. My Area of Expertise is in C#. Net, VB.Net, MS-SQL Server, ASP. Net, Silverlight, HTML, XML, Crystal Report, Active Reports, Infragistics, Component Art, ComponeOne, Lead Tools etc.