Determine if a file exists with C#
In this post, we’ll determine whether a file exists using C#.
There are several ways to check for a file’s existence in C#. There are three possibilities while testing for a file’s existence – the file exists, the file doesn’t exist, or the file’s status is unknown if the code does not have sufficient permissions to read the specified file.
To check whether the specified file exists, use the File.Exists(path) method. It returns a boolean value indicating whether the file at the specified path exists or not. The File.Exists() method returns true if the file exists and false when the file doesn’t exist or the caller does not have read access to the file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.IO; public class Example { public static void Main() { string path = @"C:\somefile.txt"; bool fileExist = File.Exists(path); if (fileExist) { Console.WriteLine("File exists."); } else { Console.WriteLine("File does not exist."); } } } |
If you want to check the presence of a directory instead, use the Directory.Exists() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; using System.IO; public class Example { public static void Main() { string path = @"C:\path\to\some\dir"; bool dirExists = Directory.Exists(path); if (dirExists) { Console.WriteLine("Directory exists."); } else { Console.WriteLine("Directory does not exist."); } } } |
To check if a file exists in a specified directory, use the following code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.IO; public class Example { public static void Main() { string path = @"C:\path\to\some\dir"; string filename = "somefile.ext"; DirectoryInfo directory = new DirectoryInfo(path); FileInfo[] files = directory.GetFiles(); bool fileFound = false; foreach (FileInfo file in files) { if (String.Compare(file.Name, filename) == 0) { fileFound = true; Console.WriteLine("File found in the specified directory!"); } } if (!fileFound) { Console.WriteLine("File does not exist in the specified directory!"); } } } |
The above solution iterates over all files present in the directory to check the existence of the specified file, which is not efficient. Here’s an alternative way which constructs the full path from the provided information:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.IO; public class Example { public static void Main() { string path = @"C:\path\to\some\dir"; string filename = "somefile.ext"; if (File.Exists(path + @"\" + filename)) { Console.WriteLine("File found in the specified directory!"); } else { Console.WriteLine("File does not exist in the specified directory!"); } } } |
To determine whether a path is a directory or a file, we can use the following code, which uses the FileAttributes class:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System; using System.IO; public class Example { public static void Main() { string path = @"C:\some\path"; try { FileAttributes attributes = File.GetAttributes(path); switch (attributes) { case FileAttributes.Directory: Console.WriteLine("The specified is a Directory."); break; default: Console.WriteLine("The specified is a file."); break; } } catch (FileNotFoundException) { Console.WriteLine("Specified path does not exist."); } } } |
That’s all about checking if a file exists with C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)