C#关于文件的创建

若使用文件夹Directory类判断了文件夹的存在,后面的文件就不需要使用File类来判断文件的存在与否和创建,只需要在前面判断过得文件夹路径后面加上文件的名字即可,它会自动判断文件是否存在,若不存在就创建。

public static void CreateFile(string appPath)

{

string fileName = @"Log\";
string path = Path.Combine(appPath, fileName);
if (!Directory.Exists(path))
{
DirectoryInfo directoryInfo = new DirectoryInfo(path);
directoryInfo.Create();

}
path = path + DateTime.Now.ToString("yyyyMMdd") + ".txt";

}

 

posted @ 2018-11-07 14:42  流年sugar  阅读(2442)  评论(0编辑  收藏  举报