C# 判断文件是否存在 - 使用 File.Exists 方法
C# 判断文件是否存在 - 使用 File.Exists 方法
要判断文件是否存在,可以使用 System.IO.File 类的 Exists 静态方法。以下是一个示例代码:
using System;
using System.IO;
public class Program
{
public static void Main(string[] args)
{
string filePath = 'path/to/file.txt';
if (File.Exists(filePath))
{
Console.WriteLine('文件存在');
}
else
{
Console.WriteLine('文件不存在');
}
}
}
在上面的代码中,我们将要判断的文件路径存储在 filePath 变量中。然后,使用 File.Exists 方法来判断文件是否存在。如果文件存在,就输出'文件存在';否则,输出'文件不存在'。
原文地址: https://www.cveoy.top/t/topic/pj5u 著作权归作者所有。请勿转载和采集!