How to use the File.Exists function in C# to determine whether a file exists
In C# file operations, determining whether a file exists is a basic functional requirement. The File.Exists function is a method in C# used to determine whether a file exists. This article will introduce how to use the File.Exists function in C# to determine whether a file exists, and provide specific code examples.
using System.IO;
string filePath = "C:\test.txt"; bool fileExists = File.Exists(filePath); if (fileExists) { Console.WriteLine("文件存在"); } else { Console.WriteLine("文件不存在"); }
In the above example, a string variable filePath is first defined to store the full path of the file. Then call the File.Exists function, pass in the file path to be determined, and assign the returned Boolean value to the fileExists variable. Finally, according to the value of fileExists, the corresponding information is output.
It should be noted that the parameter accepted by the File.Exists function is the complete path of the file, which can be an absolute path or a relative path. If you use a relative path, you need to ensure that the current working directory is consistent with the directory where the file is located.
To sum up, this article introduces how to use the File.Exists function in C# to determine whether a file exists, and provides specific code examples. Using this function can easily determine the existence of a file and provide basic support for subsequent file operations. Hope this article is helpful to you!
The above is the detailed content of How to use the File.Exists function in C# to determine whether a file exists. For more information, please follow other related articles on the PHP Chinese website!