Every once in a while you have to work with files. This article will teach you how to check wheter or not file is readable or writeable. You’re also going to learn how to check if given file or directory exists.

Quick intro to C# File, FileInfo, Directory and DirectoryInfo classes

All four classes reside inside System.IO namespace. It’s important to notice the differences between those clasesses, especially in terms of not-Info and Info classes.

The File class is used to manipulate files. You can use it to create, write to and delete a file. You can also get file attributes. It’s a static class without constructor, so you have to pass a path to a file in each method call.

The FileInfo is used to manipulate files, so you can create, write to and delete a file. This class however is not static, you have to create instance of it and you pass a path to file in constructor.

The difference between Directory and DirectoryInfo is basically the same as the difference between File and FileInfo. Former is a static class used to operate on directories and latter requires you to create instance for each path.

Check if file exists

1
var exists = System.IO.File.Exists(@"c:\file.txt");
1
2
var fileInfo = new System.IO.FileInfo(@"c:\file.txt");
var exists = fileInfo.Exists;

Check if directory exists

1
var exists = System.IO.Directory.Exists(@"d:\directory");
1
2
var directoryInfo = new System.IO.DirectoryInfo(@"d:\directory");
var exists = directoryInfo.Exists;

Test if path is a file or directory

One way to do it is to check wheter or not file/directory exists. It comes with a drawback however, because it won’t tell you explicitly if the path is a file/directory or if it simply doesn’t exist. In order to make sure that the tested path is directory (or file) and it exists we need to test it both ways. I have wrapped it all together into single method:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public enum PathType { NonExisting = 0, File = 1, Directory = 2 };

public PathType GetPathType(string path)
{
    if (File.Exists(path))
    {
        return PathType.File;
    }

    if (Directory.Exists(path))
    {
        return PathType.Directory;
    }

    return PathType.NonExisting;
}

var pathType = GetPathType(@"c:\Windows");

The second method which we can use is to get path file/directory attributes. Note that when file or directory does not exists it will throw System.IO.FileNotFoundException:

1
2
3
var attributes = File.GetAttributes(path);
var isFile = !attributes.HasFlag(FileAttributes.Directory);
var isDirectory = attributes.HasFlag(FileAttributes.Directory);

It’s also worth noting that file attributes are also exposed via FileInfo.Attributes property:

1
var attributes = new System.IO.FileInfo(@"c:\file.txt").Attributes;

Verify if file is read-only

It’s pretty easy to check read-only file flag using attributes:

1
var isReadonly = new System.IO.FileInfo(@"C:\file.txt").Attributes.HasFlag(System.IO.FileAttributes.ReadOnly);

Check if file is readable or writeable

Wheter a file can be read or not depends on multiple factors. The following example is simplified, but you should take into account that:

  • file may not exists
  • file could be locked by some other process
  • someone/something can change access to a file during execution of your applications
  • you may not have proper permissions (file may belong to other user)
  • in case of errors it will throw exceptions
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
using (var fs = new FileStream(@"C:\file.txt", FileMode.Open))
{
    var canRead = fs.CanRead;
    var canWrite = fs.CanWrite;
}

// File.Open also returns FileStream
// there are also two "shortcut" methods: File.OpenRead, File.OpenWrite
using (var fs = File.Open(@"C:\file.txt", FileMode.Open))
{
    var canRead = fs.CanRead;
    var canWrite = fs.CanWrite;
}

Also FileStream is disposeable so remember to disposed it afterwards, in this case we’re using the using statement.