C#FileHelper-文件IO
C#FileHelper
using System.IO;
namespace ZhibiXiaobai.Uril.IOHelper
{
public class FileIOHelper
{
///
/// 判断文件存不存在
/// 文件路径
/// 不存在时是否创建文件
/// 返回判断的结果
public static bool ISExists_File(string FilePath, bool IsCreate = false)
{
if (File.Exists(FilePath))
{
return true;
}
else
{
if (IsCreate) // 需不需要创建文件
{
//检验目录不存在就创建
//if (!System.IO.Directory.Exists(FilePath))
//{
// System.IO.Directory.CreateDirectory(FilePath);
//}
FilePath = FilePath.Replace(@"\\", @"/"); // 格式化目录 '\\'变为'/'
string[] ss = FilePath.Split(new char[] { '/' });
string filePathP = ss[0];
for (int i = 1; i < ss.Length - 1; i++)
{
filePathP += "/" + ss[i];
}
if (!System.IO.Directory.Exists(filePathP))
{
System.IO.Directory.CreateDirectory(filePathP);
}
File.Create(FilePath);
}
return false;
}
}
}
}