C# 文件操作的工具类
根据查阅的资料对代码进行修改并完善备注后的结果。希望能对新手有所帮助。
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# 文件操作的工具类的更多相关文章
- java中文件操作的工具类
代码: package com.lky.pojo; import java.io.BufferedReader; import java.io.BufferedWriter; import java. ...
- Java中创建操作文件和文件夹的工具类
Java中创建操作文件和文件夹的工具类 FileUtils.java import java.io.BufferedInputStream; import java.io.BufferedOutput ...
- Java操作文件夹的工具类
Java操作文件夹的工具类 import java.io.File; public class DeleteDirectory { /** * 删除单个文件 * @param fileName 要删除 ...
- 自己封装的poi操作Excel工具类
自己封装的poi操作Excel工具类 在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完 ...
- 文件上传工具类 UploadUtil.java
package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...
- Redis操作Set工具类封装,Java Redis Set命令封装
Redis操作Set工具类封装,Java Redis Set命令封装 >>>>>>>>>>>>>>>>& ...
- Redis操作List工具类封装,Java Redis List命令封装
Redis操作List工具类封装,Java Redis List命令封装 >>>>>>>>>>>>>>>> ...
- Redis操作Hash工具类封装,Redis工具类封装
Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...
- Redis操作字符串工具类封装,Redis工具类封装
Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...
随机推荐
- c# 添加reference后,Visual Studio 仍然提示无法找到函数, 询问是否添加了含有这个函数的Assembly
1.问题 添加reference后,编译仍然无法通过,测试工程添加这个assembly 就可以编译通过. 对比了这个assembly的 .net 版本,也没问题 由于工程是x64的, 添加的assem ...
- 第二十一篇 .NET高级技术之使用多线程(三)
1. 单元模式和Windows Forms 单元模式线程是一个自动线程安全机制, 非常贴近于COM——Microsoft的遗留下的组件对象模型.尽管.NET最大地放弃摆脱了遗留下的模型,但很多时候它 ...
- 慕课笔记-Java入门第三季
1.自定义异常 自定义异常必须继承Exception类或者其子类. 2.字符串 String对象创建后则不能被修改,是不可变的,所谓的修改其实是创建了新的对象. 多次创建的字符常量,Java编译程序只 ...
- wow.js零基础使用介绍
wow.js依赖于animate.css,首先需要在 head内引入animate.css或者animate.min.css wow.js或者wow.min.js,然后js文件里再写一行代码. 然后在 ...
- github网页样式
- Gitlab备份,Crontab定时备份
1:Gitlab备份非常简单,只需要一条命令就可以创建完整的备份 gitlab-rake gitlab:backup:create 使用以上命令,就相当于在/var/opt/gitlab/backup ...
- django_logging
django使用python自带的logging打印日志 logging 是线程安全的,其主要由4部分组成: Logger 用户使用的直接接口,将日志传递给Handler Handler 控制日志输出 ...
- 292. Nim游戏
292. Nim游戏 class Solution(object): def canWinNim(self, n): """ :type n: int :rtype: b ...
- mongodb Gridfs操作
GridFS 介绍 GridFS是MongoDB规范用于存储和检索大文件,如图片,音频文件,视频文件等.这是一种文件系统用来存储文件,但数据存储于MongoDB集合中.GridFS存储文件比其文档大小 ...
- 洛谷[LnOI2019]长脖子鹿省选模拟赛t1 -> 快速多项式变换
快速多项式 做法:刚拿到此题有点蒙,一开始真没想出来怎么做,于是试着去自己写几个例子. 自己枚举几种情况之后就基本看出来了,其实本题中 n 就是f(m)在m进制下的位数,每项的系数就是f(m)在m进制 ...