In this article, we will be discussing how to check if a file exists on our device using the C# programming language. Several methods to check the existence of a file will be discussed with different approaches. We’ll be using if-else checks to search for the existence of files and directories as well for locating the exact location of the file if it exists. The ternary operator or the inline if statement will also be a suitable substitute for finding the existence of a file.

Method 01: Checking If a File Exists Using the Inline If Statement in Ubuntu 20.04

In this method, we will be simplifying the process of finding the existence of a file in the current directory of the device. When we launch the command line terminal of Ubuntu 20.04, we are not present in any specific directory, so we normally point the compiler to a specific directory by using the “cd” command to navigate to a system directory present on the device. So, in this example, we will use the desktop directory as a path to get access to the files if they exist.

<img alt="Text Description automatically generated with medium confidence" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/text-description-automatically-generated-with-med.png" data-lazy- height="114" src="data:image/svg xml,” width=”571″>

In the above code, we have initialized a string variable in which we are saving the name of a text file that we need to ensure it exists in our device or not. We will utilize the “File.Exists()” function after initializing the variable and specifying it with the name of the file which is present in the “System.IO” namespace. This function is written as the condition expression to the inline if statement, and after the question mark, we have written the first and second expression in which the true and false cases were defined: the true guess was that the file exists, and the false case was that the file does not exist. The compiler will make a decision and print out the first or second expression by deciding on the availability of the file.

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/text-description-automatically-generated-79.png" data-lazy- height="56" src="data:image/svg xml,” width=”457″>

As the beforementioned named text file was saved on the desktop of this device, the code ran successfully, found the file, and printed it out as evidence that the file exists. But what if the file didn’t exist on our desktop? Then the second expression of the inline if statement would become the output, and it will be shown as the snippet below:

<img alt="A screenshot of a computer Description automatically generated with low confidence" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/a-screenshot-of-a-computer-description-automatica-5.png" data-lazy- height="55" src="data:image/svg xml,” width=”460″>

Method 02: Using the If and Else Statement to Check Whether the File Exists or not in Ubuntu 20.04

In this method, we will be using the traditional if and else statement to check whether the file exists on our desktop directory or not. Then if the check would have the “file.Exists()” as the argument of the function, while the file.Exists() will have the path of the file as an argument to determine whether the file exists or not

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/text-description-automatically-generated-80.png" data-lazy- height="151" src="data:image/svg xml,” width=”423″>

In the above piece of code, we have initialized the simple traditional if-else check-in which we will use the “file.Exists()” function as the checker of the existence of the file. The if statement will provide the true expression, and the else statement will provide the false expression. Whatever the findings would be, the outcome of the program will be printed on one of the statements as “File exists ” or “File does not exist” as we can see in the output below that the compiler was able to locate the file with the similar name and the true statement of the if and else statement is selected for printing.

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/text-description-automatically-generated-81.png" data-lazy- height="56" src="data:image/svg xml,” width=”457″>

But let us consider that if the file did not exist and we ran the same program, the output will be as shown below. For this outcome, we first had to delete that file and then run this program again to go through the false expression.

<img alt="A screenshot of a computer Description automatically generated with low confidence" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/a-screenshot-of-a-computer-description-automatica-6.png" data-lazy- height="55" src="data:image/svg xml,” width=”460″>

Method 03: Using the Not Operator for Checking the Existence of a File in Ubuntu 20.04

The if and else statement will be used again in this process, but the If check will be changed from the previous methods. In this method, we will use the not operator with the “File.Exists()” method, which is similar to the previous way, which additionally checked for the existence of a file using the if and else statements, but this method will take the path of the file as a parameter in the form of a string variable.

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/text-description-automatically-generated-82.png" data-lazy- height="198" src="data:image/svg xml,” width=”419″>

In the above C# program, we are initializing a string variable to save the path of the file to be later on called as the parameter of the function. Then we wrote the if check, in which we wrote the “File.Exists()” function with the string variable containing the path of the text file that we want to get checked. In both the if and else sections, the appropriate output message is stored for the confirmation of the result. The successful finding of the text file from the desktop is shown below:

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/text-description-automatically-generated-83.png" data-lazy- height="56" src="data:image/svg xml,” width=”457″>

After this, we will delete that text file and run this code one more time to see the “else” statement to be true and display the desired output, which can be shown below:

<img alt="A screenshot of a computer Description automatically generated with low confidence" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/a-screenshot-of-a-computer-description-automatica-7.png" data-lazy- height="55" src="data:image/svg xml,” width=”460″>

Method 04: Checking the File’s Existence and the Directory Existence of a File in Ubuntu 20.04

In this method, we will be enabling another check in the process, which is the directory check. In the method, we will use the additional “Directory.Exists()” function, which takes the path of a file as a parameter to check whether the directory of the file exists or not. The use of the “IF and else ” statement will still be constant in this method for delivering the final result.

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/text-description-automatically-generated-84.png" data-lazy- height="205" src="data:image/svg xml,” width=”423″>

In the above code, we are declaring a string variable named “path,” which will store the path of the text file that we will verify. Then in the if and else statement, we will use the double-checks of the “Directory.Exists()” function with the “File.Exists()” function. After the checks, we also wrote both the displaying messages that will be shown in both results on the display screen, covering both possibilities. After this, we shall run the program on the command line terminal of Ubuntu 20.04.

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/text-description-automatically-generated-85.png" data-lazy- height="56" src="data:image/svg xml,” width=”457″>

The above snippet shows the successful allocation of the text file, whose path we passed as an argument in the function for finding the file and the directory and proving that both checks ran successfully. Now we will have a look at the else statement of the program and see how this works if the file does not exist. Present below was the output of the program when we deleted the file before running it again.

<img alt="A screenshot of a computer Description automatically generated with low confidence" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/a-screenshot-of-a-computer-description-automatica-8.png" data-lazy- height="55" src="data:image/svg xml,” width=”460″>

Conclusion

In this article, we discussed several methods to check the existence of a file in the C# programming language and then implemented them in the Ubuntu 20.04 environment. The methods had the use of the “IF and else” statement as a common factor, but the checks kept changing their form in every method. The directory check was also viable in these methods as it helped us further justify the existence of the file. We also replaced the if and else statement in one of the methods with the “inline if statement” and implemented it with the ternary operator to check for the existence of the text file.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/Author-Image-150×150.jpg6293dcf303a10.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.