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工具类封装 >>>>>>>>>>>>>>>>>>& ...
随机推荐
- 658. Find K Closest Elements
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...
- bzoj 2039: [2009国家集训队]employ人员雇佣【最小割】
一开始在https://www.cnblogs.com/lokiii/p/10770919.html基础上连(i,j,b[i][j])建了个极丑的图T掉了--把dinic换成isap勉强能卡过 首先因 ...
- uoj#283. 直径拆除鸡(构造)
传送门 好神的构造题 vfk巨巨的题解 //minamoto #include<bits/stdc++.h> #define R register #define fp(i,a,b) fo ...
- RDS 导出Mysqlbinlog_二进制日志
1:首先进入RDS后台查看Mysqlbin_log日志,可以通过以下命令进行查看: show master logs; 或 show binary logs; 2:比如我们获取的是:mysql-bin ...
- JAVA 7新特性——在单个catch代码块中捕获多个异常,以及用升级版的类型检查重新抛出异常
在Java 7中,catch代码块得到了升级,用以在单个catch块中处理多个异常.如果你要捕获多个异常并且它们包含相似的代码,使用这一特性将会减少代码重复度.下面用一个例子来理解. Java 7之前 ...
- C#基础之析构函数
- Java集合框架常见面试题
点击关注公众号及时获取笔主最新更新文章,并可免费领取本文档配套的<Java面试突击>以及Java工程师必备学习资源. 剖析面试最常见问题之Java基础知识 说说List,Set,Map三者 ...
- 基于Matlab的标记分水岭分割算法
转自:http://blog.sina.com.cn/lyqmath 1 综述 Separating touching objects in an image is one of the more d ...
- 移动端REM布局模板(阿里高清方案)
移动端REM布局模板(阿里高清方案),蛮好的,转自: http://www.jianshu.com/p/985d26b40199 . <!DOCTYPE html> <html la ...
- (wp8.1开发)触摸键从推出变返回
---恢复内容开始--- 今天开发wp8.1,遇到这样一个问题,从第一个页面跳到第二个页面,在第二个页面按返回,结果程序退出了.原来是系统把返回键定义成退出了. 解决办法: 在第一个页面加入如下代码 ...