C#判断文件是否存在,不存在则创建!

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!");
         //执行读写操作
    }

 

posted @ 2021-07-30 15:14  Cmale  阅读(6036)  评论(0编辑  收藏  举报