根据查阅的资料对代码进行修改并完善备注后的结果。希望能对新手有所帮助。
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. TCP协议怎么关闭?

    TCP协议是一个面向连接的传输层协议,那如果避免遭到破坏,该怎样正确关闭呢?一般正常关闭TCP连接是采用四次挥手机制,其实主体就两个,客户端和服务器交互传递,且连续四次,传递的东西是FIN数据包和AC ...

  2. PHP实现用户登录页面

    PHP学习日常,放在上面记录一下咯 我用了bootstrap框架,这样的界面要好看一点 登录页面: 必须用户名.密码.验证码都输入正确才能登录成功喔,否则出现下面提示 登陆成功之后,登录和注册选项切换 ...

  3. spark sql 对接 HDFS

    上一篇博客我向大家介绍了如何快速地搭建spark run on standalone,下面我将介绍saprk sql 如何对接 hdfs 我们知道,在spark shell 中操作hdfs 上的数据是 ...

  4. 电脑内存和CPU的关系

    http://zhidao.baidu.com/link?url=OmHYd0uUJ3elyOnx1Qpdw1GGhMQBzwbdKSwR62Dn6j0090-sR0sQWR02THP-uPx7cK6 ...

  5. DRF教程3-类视图

    Rewriting our API using class-based views 使用类视图重写root views,只需要一点点重构. from django.http import Http40 ...

  6. 【aspnetcore】使用TagHelper制作分页组件

    自定义TageHelper并不难,只要记住几个点: 继承TagHelper 定义需要在TagHelper中传入的参数,如果不需要参数,可忽略 重写Process方法 在Process中拼接要输出的HT ...

  7. python tickle模块与json模块

    #! /usr/bin/env python# -*- coding:utf-8 -*-#JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式 ...

  8. vue项目打包后文本溢出代码消失问题

    补充 https://www.cnblogs.com/richard1015/p/8526988.html vue webpack 打包编译-webkit-box-orient: vertical 后 ...

  9. 实现一个redis连接池

    #*****************jedis连接参数设置*********************#redis服务器ipredis.ip=169.254.130.122#redis服务器端口号red ...

  10. django-Haystack库

    本文参考自Haystack官方文档:https://django-haystack.readthedocs.io/en/master/tutorial.html#configuration 简介 Ha ...