Home >Backend Development >C#.Net Tutorial >C# FileNotFoundException
While dealing with Files Input Output in C#, various exception might rise, but the FileNotFoundException is raised when we attempt to access a file in our program and that file does not exist or is deleted. So, basically, the FileNotFound Exception occurs when we have an address to a file in our system, but when we execute the program, the file we mentioned or passed, is not to be found. There could be multiple reasons for why this file is not found. The file maybe deleted from the location or the file name could have been changed and does not match with the names we mentioned. It is also possible when we pass a wrong address and when it hits the address, there is no file and thus the exception occurs.
Syntax:
Every method, class or exception has its standard syntax.
In case of FileNotFound Exception, the standard syntax is as follows:
public class FileNotFoundException :System.IO.IOException
The FileNotFound Exception is part of IOException, which is inherited from SystemException, going up to Exception and Object class.
Given below are the examples mentioned:
Code:
using System; using System.IO; class Program { static void Main() { try { using (StreamReaderfilereader = new StreamReader("nofile.txt")) { filereader.ReadToEnd(); } } catch (FileNotFoundException ex) { Console.WriteLine(ex); } } }
Explanation:
Output:
Here we will execute the code similar to above code, but without any try catch block, it will be simple program, where we cannot guess what exactly could go wrong.
Code:
using System.IO; using System; class Program { static void Main() { using (StreamReaderfilereader = new StreamReader("incorrectfilename.txt")) { filereader.ReadToEnd(); } } }
Explanation:
Output:
And as you can see, unlike our earlier example, this is an unhandled exception and output is as expected.
Just like any other exception, this FileNotFound Exception can be avoided. Out of the ways that we can use to avoid this exception, File.Exists method is recommended. When we are uncertain if the file we are passing in argument is not available at the source link, it is better to use File.Exists method. File.Exists method is recommended.
Example:
We will use the File.Exists method in program and see how is can be used further.
Code:
using System.IO; using System; class Program { static void Main() { bool ifexists = File.Exists("incorrectfilename.txt"); Console.WriteLine("\n "+ifexists); } }
Explanation:
Output:
To Conclude, the FileNotFound Exception comes from IO system namespace of the object class. FileNotFoundException is responsible for occurring at times when we pass a file or are attempting to execute input or output operations with file but the file does not exists. Other reasons could be, incorrect file name, or incorrect source link. File Exists method can be used to avoid this exception.
The above is the detailed content of C# FileNotFoundException. For more information, please follow other related articles on the PHP Chinese website!