C#有强大的文件和文件夹操作类,但是存在一些没有的方法,这里集成了一下:
检测指定目录是否存在
检测指定文件是否存在,如果存在返回true
获取指定目录中的文件列表
获取指定目录中所有子目录列表,若要搜索嵌套的子目录列表,请使用重载方法.
获取指定目录及子目录中所有文件列表
检测指定目录是否为空
检测指定目录中是否存在指定的文件
检测指定目录中是否存在指定的文件,若要搜索子目录请使用重载方法.
检测指定目录中是否存在指定的文件
创建目录
删除目录
删除文件
创建文件
移动文件(剪贴--粘贴)
复制文件
复制文件夹
检查文件,如果文件不存在则创建
删除指定文件夹对应其他文件夹里的文件
从文件的绝对路径中获取文件名( 包含扩展名 )
创建一个目录
创建一个文件
创建一个文件,并将字节流写入文件
获取文本文件的行数
获取一个文件的长度
获取指定目录中的子目录列表
向文本文件写入内容
向文本文件的尾部追加内容
将现有文件的内容复制到新文件中
将文件移动到指定目录
清空指定目录
清空文件内容
删除指定目录
using System;
using System.Text;
using System.IO;
namespace FileProcessHelper
{
// 文件操作类
public static class DirFile
{
// 检测指定目录是否存在
public static bool IsExistDirectory(string DirPathStr)
{
return Directory.Exists(DirPathStr);
}
// 检测指定文件是否存在,如果存在则返回true。
public static bool IsExistFile(string FilePathStr)
{
return File.Exists(FilePathStr);
}
// 获取指定目录中所有文件列表
public static string[] GetFileNames(string DirPathStr)
{
//如果目录不存在
if (!IsExistDirectory(DirPathStr))
{
throw new FileNotFoundException();
}
//获取文件列表
return Directory.GetFiles(DirPathStr);
}
// 获取指定目录中所有子目录列表,若要搜索嵌套的子目录列表,请使用重载方法.
public static string[] GetDirectories(string DirPathStr)
{
try
{
return Directory.GetDirectories(DirPathStr);
}
catch (IOException ex)
{
throw ex;
}
}
// 获取指定目录及子目录中所有文件列表
public static string[] GetFileNames(string DirPathStr, string FindPattern, bool FindNext)
{
//如果目录不存在,则抛出异常
if (!IsExistDirectory(DirPathStr))
{
throw new FileNotFoundException();
}
try
{
if (FindNext)
{
return Directory.GetFiles(DirPathStr, FindPattern, SearchOption.AllDirectories);
}
else
{
return Directory.GetFiles(DirPathStr, FindPattern, SearchOption.TopDirectoryOnly);
}
}
catch (IOException ex)
{
throw ex;
}
}
// 检测指定目录是否为空
public static bool IsEmptyDirectory(string DirPathStr)
{
try
{
//判断是否存在文件
string[] files = GetFileNames(DirPathStr);
if (files.Length > 0)
{
return false;
}
//判断是否存在文件夹
string[] directorys = GetDirectories(DirPathStr);
if (directorys.Length > 0)
{
return false;
}
return true;
}
catch
{
//这里记录日志
//LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
return true;
}
}
// 检测指定目录中是否存在指定的文件,若要搜索子目录请使用重载方法.
public static bool FileIsContains(string DirPathStr, string FindPattern)
{
try
{
//获取指定的文件列表
string[] files = GetFileNames(DirPathStr, FindPattern, false);
//判断指定文件是否存在
if (files.Length == 0)
{
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
// 检测指定目录中是否存在指定的文件
public static bool FileIsContains(string DirPathStr, string FindPattern, bool FindNext)
{
try
{
//获取指定的文件列表
string[] files = GetFileNames(DirPathStr, FindPattern, true);
//判断指定文件是否存在
if (files.Length == 0)
{
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
//LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
}
}
// 创建目录
public static void CreateDir(string dir)
{
if (dir.Length == 0) return;
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
}
// 删除目录
public static void DeleteDir(string dir)
{
if (dir.Length == 0) return;
if (Directory.Exists(dir))
Directory.Delete(dir);
}
// 删除文件
// 要删除的文件路径和名称
public static void DeleteFile(string file)
{
if (File.Exists( file))
File.Delete( file);
}
// 创建文件
public static void CreateFile(string dir, string pagestr)
{
dir = dir.Replace("/", "\");
if (dir.IndexOf("\") > -1)
CreateDir(dir.Substring(0, dir.LastIndexOf("\")));
System.IO.StreamWriter sw = new System.IO.StreamWriter( dir, false, System.Text.Encoding.GetEncoding("GB2312"));
sw.Write(pagestr);
sw.Close();
}
// 移动文件(剪贴--粘贴)
public static void MoveFile(string dir1, string dir2)
{
dir1 = dir1.Replace("/", "\");
dir2 = dir2.Replace("/", "\");
if (File.Exists( dir1))
File.Move( dir1, dir2);
}
// 复制文件
public static void CopyFile(string dir1, string dir2)
{
dir1 = dir1.Replace("/", "\");
dir2 = dir2.Replace("/", "\");
if (File.Exists( dir1))
{
File.Copy( dir1, dir2, true);
}
}
// 根据时间得到目录名yyyyMMdd
public static string GetDateDir()
{
return DateTime.Now.ToString("yyyyMMdd");
}
// 根据时间得到文件名HHmmssff
public static string GetDateFile()
{
return DateTime.Now.ToString("HHmmssff");
}
// 复制文件夹(递归)
public static void CopyFolder(string varFromDirectory, string varToDirectory)
{
Directory.CreateDirectory(varToDirectory);
if (!Directory.Exists(varFromDirectory)) return;
string[] directories = Directory.GetDirectories(varFromDirectory);
if (directories.Length > 0)
{
foreach (string d in directories)
{
CopyFolder(d, varToDirectory + d.Substring(d.LastIndexOf("\")));
}
}
string[] files = Directory.GetFiles(varFromDirectory);
if (files.Length > 0)
{
foreach (string s in files)
{
File.Copy(s, varToDirectory + s.Substring(s.LastIndexOf("\")), true);
}
}
}
// 检查文件,如果文件不存在则创建
public static void ExistsFile(string FilePath)
{
if (!File.Exists(FilePath))
{
FileStream fs = File.Create(FilePath);
fs.Close();
}
}
// 删除指定文件夹对应其他文件夹里的文件
public static void DeleteFolderFiles(string varFromDirectory, string varToDirectory)
{
Directory.CreateDirectory(varToDirectory);
if (!Directory.Exists(varFromDirectory)) return;
string[] directories = Directory.GetDirectories(varFromDirectory);
if (directories.Length > 0)
{
foreach (string d in directories)
{
DeleteFolderFiles(d, varToDirectory + d.Substring(d.LastIndexOf("\")));
}
}
string[] files = Directory.GetFiles(varFromDirectory);
if (files.Length > 0)
{
foreach (string s in files)
{
File.Delete(varToDirectory + s.Substring(s.LastIndexOf("\")));
}
}
}
// 从文件的绝对路径中获取文件名( 包含扩展名 )
public static string GetFileName(string FilePathStr)
{
//获取文件的名称
FileInfo fi = new FileInfo(FilePathStr);
return fi.Name;
}
// 创建一个目录
public static void CreateDirectory(string DirPathStr)
{
//如果目录不存在则创建该目录
if (!IsExistDirectory(DirPathStr))
{
Directory.CreateDirectory(DirPathStr);
}
}
// 创建一个文件。
public static void CreateFile(string FilePathStr)
{
try
{
//如果文件不存在则创建该文件
if (!IsExistFile(FilePathStr))
{
//创建一个FileInfo对象
FileInfo file = new FileInfo(FilePathStr);
//创建文件
FileStream fs = file.Create();
//关闭文件流
fs.Close();
}
}
catch (Exception ex)
{
//LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
throw ex;
}
}
// 创建一个文件,并将字节流写入文件。
public static void CreateFile(string FilePathStr, byte[] buffer)
{
try
{
//如果文件不存在则创建该文件
if (!IsExistFile(FilePathStr))
{
//创建一个FileInfo对象
FileInfo file = new FileInfo(FilePathStr);
//创建文件
FileStream fs = file.Create();
//写入二进制流
fs.Write(buffer, 0, buffer.Length);
//关闭文件流
fs.Close();
}
}
catch (Exception ex)
{
//LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
throw ex;
}
}
// 获取文本文件的行数
public static int GetLineCount(string FilePathStr)
{
//将文本文件的各行读到一个字符串数组中
string[] rows = File.ReadAllLines(FilePathStr);
//返回行数
return rows.Length;
}
// 获取一个文件的长度,单位为Byte
public static int GetFileSize(string FilePathStr)
{
//创建一个文件对象
FileInfo fi = new FileInfo(FilePathStr);
//获取文件的大小
return (int)fi.Length;
}
// 获取指定目录及子目录中所有子目录列表
public static string[] GetDirectories(string DirPathStr, string FindPattern, bool FindNext)
{
try
{
if (FindNext)
{
return Directory.GetDirectories(DirPathStr, FindPattern, SearchOption.AllDirectories);
}
else
{
return Directory.GetDirectories(DirPathStr, FindPattern, SearchOption.TopDirectoryOnly);
}
}
catch (IOException ex)
{
throw ex;
}
}
// 向文本文件中写入内容
public static void WriteText(string FilePathStr, string text, Encoding encoding)
{
//向文件写入内容
File.WriteAllText(FilePathStr, text, encoding);
}
// 向文本文件的尾部追加内容
public static void AppendText(string FilePathStr, string content)
{
File.AppendAllText(FilePathStr, content);
}
// 将源文件的内容复制到目标文件中
public static void Copy(string sourceFilePath, string destFilePath)
{
File.Copy(sourceFilePath, destFilePath, true);
}
// 将文件移动到指定目录
public static void Move(string sourceFilePath, string descDirectoryPath)
{
//获取源文件的名称
string sourceFileName = GetFileName(sourceFilePath);
if (IsExistDirectory(descDirectoryPath))
{
//如果目标中存在同名文件,则删除
if (IsExistFile(descDirectoryPath + "\" + sourceFileName))
{
DeleteFile(descDirectoryPath + "\" + sourceFileName);
}
//将文件移动到指定目录
File.Move(sourceFilePath, descDirectoryPath + "\" + sourceFileName);
}
}
// 从文件的绝对路径中获取文件名( 不包含扩展名 )
public static string GetFileNameNoExtension(string FilePathStr)
{
//获取文件的名称
FileInfo fi = new FileInfo(FilePathStr);
return fi.Name.Split('.')[0];
}
// 从文件的绝对路径中获取扩展名
public static string GetExtension(string FilePathStr)
{
//获取文件的名称
FileInfo fi = new FileInfo(FilePathStr);
return fi.Extension;
}
// 清空指定目录下所有文件及子目录,但该目录依然保存.
public static void ClearDirectory(string DirPathStr)
{
if (IsExistDirectory(DirPathStr))
{
//删除目录中所有的文件
string[] files = GetFileNames(DirPathStr);
for (int i = 0; i < files.Length; i++)
{
DeleteFile(files[i]);
}
//删除目录中所有的子目录
string[] directorys = GetDirectories(DirPathStr);
for (int i = 0; i < directorys.Length; i++)
{
DeleteDirectory(directorys[i]);
}
}
}
// 清空文件内容
public static void ClearFile(string FilePathStr)
{
//删除文件
File.Delete(FilePathStr);
//重新创建该文件
CreateFile(FilePathStr);
}
// 删除指定目录及其所有子目录
public static void DeleteDirectory(string DirPathStr)
{
if (IsExistDirectory(DirPathStr))
{
Directory.Delete(DirPathStr, true);
}
}
}
}



