C# 判断文件/文件夹 是否存在

C# 寻梦 5年前 (2019-10-11) 1290次浏览 0个评论 扫描二维码

1、判断文件夹是否存在

//spath:文件夹路径名
     using System.IO;
if (Directory.Exists(spath))
{
}
else
{
    DirectoryInfo directoryInfo = new DirectoryInfo(spath);
    directoryInfo.Create();
}

2、判断文件是否存在

// filePath 文件路径名
if (!File.Exists(filePath))
{
    //MessageBox.Show(filePath + "  not exists!");
    FileStream fs = File.Create(filePath);//创建文件
    fs.Close();
    return ;
}
else
{
     MessageBox.Show(filePath + "  exists!");
     //执行读写操作
}
喜欢 (2)
[支付宝扫码,感谢支持]
分享 (0)
关于作者:

您必须 登录 才能发表评论!