根据查阅的资料对代码进行修改并完善备注后的结果。希望能对新手有所帮助。
1 using System; using System.IO;
namespace 文件操作类
{
public class FileHelper
{
/// <summary>
/// 判断文件是否存在
/// </summary>
/// <param name="filePath">文件全路径</param>
/// <returns></returns>
public static bool Exists(string filePath)
{
if (filePath == null || filePath.Trim() == "")
{
return false;
} if (File.Exists(filePath))
{
return true;
} return false;
} /// <summary>
/// 创建文件夹
/// </summary>
/// <param name="dirPath">文件夹路径</param>
/// <returns></returns>
public static bool CreateDir(string dirPath)
{
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
return true;
} /// <summary>
/// 创建文件
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
public static bool CreateFile(string filePath)
{
if (!File.Exists(filePath))
{
FileStream fs = File.Create(filePath);
fs.Close();
fs.Dispose();
}
return true; } /// <summary>
/// 读文件内容
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="encoding">编码格式</param>
/// <returns></returns>
public static string Read(string filePath,Encoding encoding)
{
if (!Exists(filePath))
{
return null;
}
//将文件信息读入流中
using (FileStream fs = new FileStream(filePath,FileMode.Open))
{
return new StreamReader(fs, encoding).ReadToEnd();
}
} /// <summary>
/// 读取文件的一行内容
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="encoding">编码格式</param>
/// <returns></returns>
public static string ReadLine(string filePath, Encoding encoding)
{
if (!Exists(filePath))
{
return null;
}
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
return new StreamReader(fs, encoding).ReadLine();
}
} /// <summary>
/// 写文件
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="content">文件内容</param>
/// <returns></returns>
public static bool Write(string filePath, string content)
{
if (!Exists(filePath) || content == null)
{
return false;
} //将文件信息读入流中
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
{
lock (fs)//锁住流
{
if (!fs.CanWrite)
{
throw new System.Security.SecurityException("文件filePath=" + filePath + "是只读文件不能写入!");
} byte[] buffer = Encoding.Default.GetBytes(content);
fs.Write(buffer, , buffer.Length);
return true;
}
}
} /// <summary>
/// 写入一行
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="content">内容</param>
/// <returns></returns>
public static bool WriteLine(string filePath, string content)
{
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate | FileMode.Append))
{
lock (fs)
{
if (!fs.CanWrite)
{
throw new System.Security.SecurityException("文件filePath=" + filePath + "是只读文件不能写入!");
} StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(content);
sw.Dispose();
sw.Close();
return true;
}
}
} public static bool CopyDir(DirectoryInfo fromDir, string toDir)
{
return CopyDir(fromDir, toDir, fromDir.FullName);
} /// <summary>
/// 复制目录
/// </summary>
/// <param name="fromDir">被复制的目录路径</param>
/// <param name="toDir">复制到的目录路径</param>
/// <returns></returns>
public static bool CopyDir(string fromDir, string toDir)
{
if (fromDir == null || toDir == null)
{
throw new NullReferenceException("参数为空");
} if (fromDir == toDir)
{
throw new Exception("两个目录都是" + fromDir);
} if (!Directory.Exists(fromDir))
{
throw new IOException("目录fromDir=" + fromDir + "不存在");
} DirectoryInfo dir = new DirectoryInfo(fromDir);
return CopyDir(dir, toDir, dir.FullName);
} /// <summary>
/// 复制目录
/// </summary>
/// <param name="fromDir">被复制的目录路径</param>
/// <param name="toDir">复制到的目录路径</param>
/// <param name="rootDir">被复制的根目录路径</param>
/// <returns></returns>
private static bool CopyDir(DirectoryInfo fromDir, string toDir, string rootDir)
{
string filePath = string.Empty;
foreach (FileInfo f in fromDir.GetFiles())
{
filePath = toDir + f.FullName.Substring(rootDir.Length);
string newDir = filePath.Substring(, filePath.LastIndexOf("\\"));
CreateDir(newDir);
File.Copy(f.FullName, filePath, true);
} foreach (DirectoryInfo dir in fromDir.GetDirectories())
{
CopyDir(dir, toDir, rootDir);
} return true;
} /// <summary>
/// 删除文件
/// </summary>
/// <param name="filePath">文件的完整路径</param>
/// <returns></returns>
public static bool DeleteFile(string filePath)
{
if (Exists(filePath))
{
File.Delete(filePath);
return true;
}
return false;
} public static void DeleteDir(DirectoryInfo dir)
{
if (dir == null)
{
throw new NullReferenceException("目录不存在");
} foreach (DirectoryInfo d in dir.GetDirectories())
{
DeleteDir(d);
} foreach (FileInfo f in dir.GetFiles())
{
DeleteFile(f.FullName);
} dir.Delete(); } /// <summary>
/// 删除目录
/// </summary>
/// <param name="dir">指定目录路径</param>
/// <param name="onlyDir">是否只删除目录</param>
/// <returns></returns>
public static bool DeleteDir(string dir, bool onlyDir)
{
if (dir == null || dir.Trim() == "")
{
throw new NullReferenceException("目录dir=" + dir + "不存在");
} if (!Directory.Exists(dir))
{
return false;
} DirectoryInfo dirInfo = new DirectoryInfo(dir);
if (dirInfo.GetFiles().Length == && dirInfo.GetDirectories().Length == )
{
Directory.Delete(dir);
return true;
} if (!onlyDir)
{
return false;
}
else
{
DeleteDir(dirInfo);
return true;
} } /// <summary>
/// 在指定的目录中查找文件
/// </summary>
/// <param name="dir">目录路径</param>
/// <param name="fileName">文件名</param>
/// <returns></returns>
public static bool FindFile(string dir, string fileName)
{
if (dir == null || dir.Trim() == "" || fileName == null || fileName.Trim() == "" || !Directory.Exists(dir))
{
return false;
} DirectoryInfo dirInfo = new DirectoryInfo(dir);
return FindFile(dirInfo, fileName); } public static bool FindFile(DirectoryInfo dir, string fileName)
{
foreach (DirectoryInfo d in dir.GetDirectories())
{
if (File.Exists(d.FullName + "\\" + fileName))
{
return true;
}
FindFile(d, fileName);
} return false;
} }
}

转载请标出本博地址:http://www.cnblogs.com/codeToUp/p/4793153.html

C# 文件操作的工具类的更多相关文章

  1. java中文件操作的工具类

    代码: package com.lky.pojo; import java.io.BufferedReader; import java.io.BufferedWriter; import java. ...

  2. Java中创建操作文件和文件夹的工具类

    Java中创建操作文件和文件夹的工具类 FileUtils.java import java.io.BufferedInputStream; import java.io.BufferedOutput ...

  3. Java操作文件夹的工具类

    Java操作文件夹的工具类 import java.io.File; public class DeleteDirectory { /** * 删除单个文件 * @param fileName 要删除 ...

  4. 自己封装的poi操作Excel工具类

    自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...

  5. 文件上传工具类 UploadUtil.java

    package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

  6. Redis操作Set工具类封装,Java Redis Set命令封装

    Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...

  7. Redis操作List工具类封装,Java Redis List命令封装

    Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...

  8. Redis操作Hash工具类封装,Redis工具类封装

    Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...

  9. Redis操作字符串工具类封装,Redis工具类封装

    Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...

随机推荐

  1. U3D手游《苍穹变》性能优化经验谈

    4月11日,由unity公司举办的Unite 2016大会在上海正式举行,在4月12日的案例分享专场会议上,天神互动U3D高级开发工程师康凯以手游<苍穹变>为例讲述了3DMMOARPG游戏 ...

  2. JSP技术概念

  3. 字符条件变成in条件格式数据

    private string getInQuerySql(string query) { string resulr = ""; foreach (var item in quer ...

  4. JMeter提取和重用作为变量 - 具有更多提取器

    这是我们最受欢迎的博文,我们添加了更多提取器.这篇文章解释了如何使用正则表达式提取器从第一个请求的响应中提取密钥,并将提取的密钥用于后续请求.我们称之为JMeter Extract并重复使用. 现在您 ...

  5. Action类为何要 extends ActionSupport以及实现ModelDriven

    http://blog.sina.com.cn/s/blog_164e377490102wqhk.html

  6. Ubuntu-apt安装Jenkins

    系统环境: Ubuntu 16.0.4 2CPU,8G 1.默认Ubuntu软件包里没有Jenkins 2.系统里添加存储密钥 wget -q -O - https://pkg.jenkins.io/ ...

  7. 543 Diameter of Binary Tree 二叉树的直径

    给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树          1         / \        2 ...

  8. MQTT进阶篇

            我们介绍了最流行的物联网协议MQTT的背景以及基本使用方法.在这篇文章中,我们会继续考察MQTT的高级玩法——与网页应用的交互.MQTT是基于TCP协议实现,基于HTTP的网页应用便无 ...

  9. VS中以插件开发的思想开发Winform应用

    简单定义: 插件(也称构件)式开发:主要内容就是一个宿主程序加上后期开发的若干插件程序构成整个系统! 宿主程序提供接口注册,插件注册实现接口,从而使不同的插件提供新的功能: 举例: 以下是用VS中的W ...

  10. css命名规范—CSS样式命名整理

    CSS样式命名整理 页面结构 容器: container/wrap整体宽度:wrapper页头:header内容:content页面主体:main页尾:footer导航:nav侧栏:sidebar栏目 ...