A few days ago, when I was looking for a method to determine whether a file exists, I stepped on a few pits and found two methods, File.Exists() and Directory.Exists() (both refer to the System.IO namespace). These two methods are easy confused. The difference in usage is as follows:
- File.Exists()
if(File.Exists(path+".文件后缀名")) //该方法是判断该路径下有没有该文件,注意一定要加上文件后缀名
{
}
else
{
FileStream fs = File.Create(path);//创建文件
fs.Close();
fs.Dispose();
return ;
}
- Directory.Exists()
if(Directory.Exists(path)) //该方法是判断该路径下有没有该文件夹,注意并不能判断某个文件是否存在
{
}
else//如果不存在
{
Directory.CreateDirectory(path);//创建这个文件夹
}
Reference: link