Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if file can be read

Tags:

c#

io

file-io

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?

like image 290
Pawan Nogariya Avatar asked Jun 26 '13 11:06

Pawan Nogariya


People also ask

How do you check if a file is readable or not?

To check read property we use canRead() method and for write, we use canWrite() method of File class. The canRead() and canWrite() methods both returns a boolean value either true or false. If the file is readable or writeable then we get true from both methods.

How do you check if a file can be read in Python?

The readable() method returns True if the file is readable, False if not.

How do you check if a file exists and is readable in bash?

To check if the a file is readable, in other words if the file has read permissions, using bash scripting, use [ -r FILE ] expression with bash if statement.

How can I tell if a file is writable in Ubuntu?

-r FILE True if file is readable by you. -s FILE True if file exists and is not empty. -w FILE True if the file is writable by you. should it not be if [[ -r $FILE ]] && [[ -w $FILE ]] instead of if [[ -r $FILE && -w $FILE ]] ?


2 Answers

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.

like image 190
Pieter van Ginkel Avatar answered Oct 05 '22 08:10

Pieter van Ginkel


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
  }
like image 41
Dmitry Bychenko Avatar answered Oct 05 '22 09:10

Dmitry Bychenko