c#语言怎么判断本地的某个文件是否存在?

2025-03-12 16:07:20
推荐回答(3个)
回答1:

方法很多但都要使用 System.IO 命名空间
给你两个做参考:

//======== 1. 方法一(要使用存在的文件信息) =======
FileInfo file = new FileInfo(sFilePath);
if (file.Exists)
{
//文件存在
string sName = file.Name;
}
else
{
//文件不存在
}

//======== 2. 方法二(只要判断文件的存在性) =======
if (File.Exists(sFilePath))
{
//文件存在
}
else
{
//文件不存在
}

回答2:

可以使用如下代码进行判断
string filepath =@"D:\dd.jpg";
if (System.IO.File.Exists(filepath))
{
MessageBox.Show("存在");
}
else
{
MessageBox.Show("不存在");
}

回答3:

System.IO.File.Exists
这个方法就可以