Check if file can be read

22,306

Solution 1

Whether a file can be read depends on a number of factors: do you have permissions, whether the hard disk is broken. I would probably have gone the same route as you did.

However, you do have to keep in mind that the information you get from this method is just a snapshot. If immediately after you call this method, someone changes the permissions on the file, accessing the file later in your code will still fail. You should not depend on the result of this method.

Just a suggestion, the following code does the same but is a bit more concise:

try
{
    File.Open(this.DataSourceFileName, FileMode.Open, FileAccess.Read).Dispose();
    return true;
}
catch (IOException)
{
    return false;
}

Since you're not really using the stream, you don't have to hold on a reference to it. Instead, you can just immediately dispose of the stream by calling dispose on the result of File.Open().

EDIT:

See https://gist.github.com/pvginkel/56658191c6bf7dac23b3893fa59a35e8 for an explanation on why I've put the Dispose() at the end of the File.Open() instead of using the using statement.

Solution 2

If you want to check for exceptions, just add appropriate try..catch to Dan Dinu code e.g.

  try {
    using (FileStream stream = File.Open(this.DataSourceFileName, FileMode.Open, FileAccess.Read)) {
      ... // <- Do something with the opened file
      return true; // <- File has been opened
    }
  }
  catch (IOException) {
    return false; // <- File failed to open
  }
Share:
22,306
Pawan Nogariya
Author by

Pawan Nogariya

I am over 13 years experienced ASP.Net Programmer. Throughout my carrier I have been involved in web development projects using Microsoft .net platform ranging from static client application to rich interface dynamic client server applications, web services etc. I have gained good experience in all the levels of web development Front end designing using HTML, CSS, LESS, Frameworks like Bootstrap Front end programming using Javascript, Jquery, AngularJS, Angular, External APIs (Select2, Datatables, TreeTable etc.), DevExpress Server side programming using ASP.Net Web forms, ASP.Net MVC, C#.Net, ADO.Net, NUnit, WCF Database level using SQL Sever Source code management - Git, Microsoft team foundation server, SVN (Subversion) Devops - Gitlab I have worked both as a freelancer and as an employee of a software company which has given me exposure to quite a lot of best practices. I also own following certificates MCPD - Microsoft Certified Professional Developer for .Net Framework 3.5 SCJP - Sun Certified Professional Developer. IBM Certified for for WebSphere V5.0. less

Updated on July 09, 2022

Comments

  • Pawan Nogariya
    Pawan Nogariya almost 2 years

    This is how I am trying to check if I can read the file before actually reading it

    FileStream stream = new FileStream();
    try
    {
       // try to open the file to check if we can access it for read
       stream = File.Open(this.DataSourceFileName, FileMode.Open, FileAccess.Read);
    }
    catch (IOException ex)
    {
       return false;
    }
    finally
    {
       stream.Dispose();
    }
    

    Is this the right way?

    Also is File.Open similar to File.ReadAllText, what I mean is, are they equally expensive for performance?