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

时间:2025-01-25 08:35:02

本章讲述:C#判断指定目录是否存在,判断文件是否存在,不存在则创建

1、判断文件夹是否存在

	//spath:文件夹路径名
	using ;
	if ((spath))
	{

	}
	else
	{
		DirectoryInfo directoryInfo = new DirectoryInfo(spath);
		();
	}

2、判断文件是否存在

	// filePath 文件路径名

	if (!(filePath))
	{
		//(filePath + "  not exists!");
		FileStream fs = (filePath);//创建文件
		();
		return ;
	}
	else
	{
		 (filePath + "  exists!");
		 //执行读写操作
	}