C# Check If the File Exists
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.
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.
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:
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
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.
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.
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.
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:
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:
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.
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.
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.
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.