C#文件非占用读取与帮助类FileHelper怎么用
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇内容主要讲解"C#文件非占用读取与帮助类FileHelper怎么用",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C#文件非占用读取与帮助类FileHe
千家信息网最后更新 2026年11月01日C#文件非占用读取与帮助类FileHelper怎么用
本篇内容主要讲解"C#文件非占用读取与帮助类FileHelper怎么用",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"C#文件非占用读取与帮助类FileHelper怎么用"吧!
非占用方式:
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);//按行读取 string strLine = sr.ReadLine();//关闭读写流和文件流sr.Close();fs.Close();
占用方式:
FileStream fs = new FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);//按行读取 string strLine = sr.ReadLine();//关闭读写流和文件流sr.Close();fs.Close();
文件以及文件夹帮助类(FileHelper)
代码:
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FileHelperDemo{ /// /// 文件操作帮助类 /// public class FileUtilHelper { #region 检测指定目录是否存在 /// /// 检测指定目录是否存在,如果存在则返回true。 /// /// 目录的绝对路径 public static bool IsExistDirectory(string directoryPath) { return Directory.Exists(directoryPath); } #endregion #region 检测指定文件是否存在 /// /// 检测指定文件是否存在,如果存在则返回true。 /// /// 文件的绝对路径 public static bool IsExistFile(string filePath) { return File.Exists(filePath); } #endregion #region 检测指定目录是否为空 /// /// 检测指定目录是否为空 /// /// 指定目录的绝对路径 public static bool IsEmptyDirectory(string directoryPath) { try { //判断是否存在文件 string[] fileNames = GetFileNames(directoryPath); if (fileNames.Length > 0) { return false; } //判断是否存在文件夹 string[] directoryNames = GetDirectories(directoryPath); if (directoryNames.Length > 0) { return false; } return true; } catch (Exception ex) { throw ex; } } #endregion #region 检测指定目录中是否存在指定的文件 /// /// 检测指定目录中是否存在指定的文件,若要搜索子目录请使用重载方法. /// /// 指定目录的绝对路径 /// 模式字符串,"*"代表0或N个字符,"?"代表1个字符。 /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。 public static bool IsContainsFile(string directoryPath, string searchPattern) { try { //获取指定的文件列表 string[] fileNames = GetFileNames1(directoryPath, searchPattern, false); //判断指定文件是否存在 if (fileNames.Length == 0) { return false; } else { return true; } } catch (Exception ex) { throw ex; } } public static string[] GetFileNames1(string directoryPath, string searchPattern, bool isSearchChild) { //如果目录不存在,则抛出异常 if (!IsExistDirectory(directoryPath)) { throw new FileNotFoundException(); } try { if (isSearchChild) { return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories); } else { return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly); } } catch (IOException ex) { throw ex; } } /// /// 检测指定目录中是否存在指定的文件 /// /// 指定目录的绝对路径 /// 模式字符串,"*"代表0或N个字符,"?"代表1个字符。 /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。 /// 是否搜索子目录 public static bool IsContainsFile(string directoryPath, string searchPattern, bool isSearchChild) { try { //获取指定的文件列表 string[] fileNames = GetFileNames2(directoryPath, searchPattern, true); //判断指定文件是否存在 if (fileNames.Length == 0) { return false; } else { return true; } } catch (Exception ex) { throw ex; } } public static string[] GetFileNames2(string directoryPath, string searchPattern, bool isSearchChild) { //如果目录不存在,则抛出异常 if (!IsExistDirectory(directoryPath)) { throw new FileNotFoundException(); } try { if (isSearchChild) { return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories); } else { return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly); } } catch (IOException ex) { throw ex; } } #endregion #region 创建一个目录 /// /// 创建一个目录 /// /// 目录的绝对路径 public static void CreateDirectory(string directoryPath) { //如果目录不存在则创建该目录 if (!IsExistDirectory(directoryPath)) { Directory.CreateDirectory(directoryPath); } } #endregion #region 创建一个文件 /// /// 创建一个文件 /// /// 文件的绝对路径 public static void CreateFile(string filePath) { try { //如果文件不存在则创建该文件 if (!IsExistFile(filePath)) { //创建一个FileInfo对象 FileInfo file = new FileInfo(filePath); //创建文件 FileStream fs = file.Create(); //关闭文件流 fs.Close(); } } catch (Exception ex) { throw ex; } } /// /// 创建一个文件,并将字节流写入文件。 /// /// 文件的绝对路径 /// 二进制流数据 public static void CreateFile(string filePath, byte[] buffer) { try { //如果文件不存在则创建该文件 if (!IsExistFile(filePath)) { //创建一个FileInfo对象 FileInfo file = new FileInfo(filePath); //创建文件 FileStream fs = file.Create(); //写入二进制流 fs.Write(buffer, 0, buffer.Length); //关闭文件流 fs.Close(); } } catch (Exception ex) { throw ex; } } #endregion #region 获取文本文件的行数 /// /// 获取文本文件的行数 /// /// 文件的绝对路径 public static int GetLineCount(string filePath) { //将文本文件的各行读到一个字符串数组中 string[] rows = File.ReadAllLines(filePath); //返回行数 return rows.Length; } #endregion #region 获取一个文件的长度 /// /// 获取一个文件的长度,单位为Byte /// /// 文件的绝对路径 public static int GetFileSize(string filePath) { //创建一个文件对象 FileInfo fi = new FileInfo(filePath); //获取文件的大小 return (int)fi.Length; } #endregion #region 获取指定目录中的文件列表 /// /// 获取指定目录中所有文件列表 /// /// 指定目录的绝对路径 public static string[] GetFileNames(string directoryPath) { //如果目录不存在,则抛出异常 if (!IsExistDirectory(directoryPath)) { throw new FileNotFoundException(); } //获取文件列表 return Directory.GetFiles(directoryPath); } /// /// 获取指定目录及子目录中所有文件列表 /// /// 指定目录的绝对路径 /// 模式字符串,"*"代表0或N个字符,"?"代表1个字符。 /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。 /// 是否搜索子目录 public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild) { //如果目录不存在,则抛出异常 if (!IsExistDirectory(directoryPath)) { throw new FileNotFoundException(); } try { if (isSearchChild) { return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories); } else { return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly); } } catch (IOException ex) { throw ex; } } #endregion #region 获取指定目录中的子目录列表 /// /// 获取指定目录中所有子目录列表,若要搜索嵌套的子目录列表,请使用重载方法. /// /// 指定目录的绝对路径 public static string[] GetDirectories(string directoryPath) { try { return Directory.GetDirectories(directoryPath); } catch (IOException ex) { throw ex; } } /// /// 获取指定目录及子目录中所有子目录列表 /// /// 指定目录的绝对路径 /// 模式字符串,"*"代表0或N个字符,"?"代表1个字符。 /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。 /// 是否搜索子目录 public static string[] GetDirectories(string directoryPath, string searchPattern, bool isSearchChild) { try { if (isSearchChild) { return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.AllDirectories); } else { return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.TopDirectoryOnly); } } catch (IOException ex) { throw ex; } } #endregion #region 向文本文件写入内容 /// /// 向文本文件中写入内容 /// /// 文件的绝对路径 /// 写入的内容 public static void WriteText(string filePath, string content) { //向文件写入内容 File.WriteAllText(filePath, content); } #endregion #region 向文本文件的尾部追加内容 /// /// 向文本文件的尾部追加内容 /// /// 文件的绝对路径 /// 写入的内容 public static void AppendText(string filePath, string content) { File.AppendAllText(filePath, content); } #endregion #region 将现有文件的内容复制到新文件中 /// /// 将源文件的内容复制到目标文件中 /// /// 源文件的绝对路径 /// 目标文件的绝对路径 public static void Copy(string sourceFilePath, string destFilePath) { File.Copy(sourceFilePath, destFilePath, true); } #endregion #region 将文件移动到指定目录 /// /// 将文件移动到指定目录 /// /// 需要移动的源文件的绝对路径 /// 移动到的目录的绝对路径 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); } } #endregion #region 从文件的绝对路径中获取文件名( 包含扩展名 ) /// /// 从文件的绝对路径中获取文件名( 包含扩展名 ) /// /// 文件的绝对路径 public static string GetFileName(string filePath) { //获取文件的名称 FileInfo fi = new FileInfo(filePath); return fi.Name; } #endregion #region 从文件的绝对路径中获取文件名( 不包含扩展名 ) /// /// 从文件的绝对路径中获取文件名( 不包含扩展名 ) /// /// 文件的绝对路径 public static string GetFileNameNoExtension(string filePath) { //获取文件的名称 FileInfo fi = new FileInfo(filePath); return fi.Name.Split('.')[0]; } #endregion #region 从文件的绝对路径中获取扩展名 /// /// 从文件的绝对路径中获取扩展名 /// /// 文件的绝对路径 public static string GetExtension(string filePath) { //获取文件的名称 FileInfo fi = new FileInfo(filePath); return fi.Extension; } #endregion #region 将文件读取到缓冲区中 /// /// 将文件读取到缓冲区中 /// /// 文件的绝对路径 public static byte[] FileToBytes(string filePath) { //获取文件的大小 int fileSize = GetFileSize(filePath); //创建一个临时缓冲区 byte[] buffer = new byte[fileSize]; //创建一个文件流 FileInfo fi = new FileInfo(filePath); FileStream fs = fi.Open(FileMode.Open); try { //将文件流读入缓冲区 fs.Read(buffer, 0, fileSize); return buffer; } catch (IOException ex) { throw ex; } finally { //关闭文件流 fs.Close(); } } #endregion #region 将文件读取到字符串中 /// /// 将文件读取到字符串中 /// /// 文件的绝对路径 /// 字符编码 public static string FileToString(string filePath, Encoding encoding) { //创建流读取器 StreamReader reader = new StreamReader(filePath, encoding); try { //读取流 return reader.ReadToEnd(); } catch (Exception ex) { throw ex; } finally { //关闭流读取器 reader.Close(); } } #endregion #region 清空指定目录 /// /// 清空指定目录下所有文件及子目录,但该目录依然保存. /// /// 指定目录的绝对路径 public static void ClearDirectory(string directoryPath) { if (IsExistDirectory(directoryPath)) { //删除目录中所有的文件 string[] fileNames = GetFileNames(directoryPath); for (int i = 0; i < fileNames.Length; i++) { DeleteFile(fileNames[i]); } //删除目录中所有的子目录 string[] directoryNames = GetDirectories(directoryPath); for (int i = 0; i < directoryNames.Length; i++) { DeleteDirectory(directoryNames[i]); } } } #endregion #region 清空文件内容 /// /// 清空文件内容 /// /// 文件的绝对路径 public static void ClearFile(string filePath) { //删除文件 File.Delete(filePath); //重新创建该文件 CreateFile(filePath); } #endregion #region 删除指定文件 /// /// 删除指定文件 /// /// 文件的绝对路径 public static void DeleteFile(string filePath) { if (IsExistFile(filePath)) { File.Delete(filePath); } } #endregion #region 删除指定目录 /// /// 删除指定目录及其所有子目录 /// /// 指定目录的绝对路径 public static void DeleteDirectory(string directoryPath) { if (IsExistDirectory(directoryPath)) { Directory.Delete(directoryPath, true); } } #endregion #region 将字节(B)转换成兆(M),并保留两位小数 /// /// 将字节(B)转换成兆(M),并保留两位小数 /// public static string ConvertByteToMB(int byteLength) { return Math.Round((double)byteLength / (1024 * 1024), 2).ToString() + "M"; } #endregion #region 去除文件名中的特殊字符 public static string ReplaceBadCharOfFileName(string fileName) { fileName = fileName.Replace("\\", string.Empty); fileName = fileName.Replace("/", string.Empty); fileName = fileName.Replace(":", string.Empty); fileName = fileName.Replace("*", string.Empty); fileName = fileName.Replace("?", string.Empty); fileName = fileName.Replace("\"", string.Empty); fileName = fileName.Replace("<", string.Empty); fileName = fileName.Replace(">", string.Empty); fileName = fileName.Replace("|", string.Empty); fileName = fileName.Replace(" ", string.Empty); return fileName.ToString(); } #endregion }}到此,相信大家对"C#文件非占用读取与帮助类FileHelper怎么用"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
文件
目录
路径
字符
内容
子目
子目录
搜索
检测
代表
字符串
文本
扩展名
帮助
文件名
移动
名称
开头
模式
源文件
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
湖北九亚互联网络科技有限公司
广东万易互联网科技有限公司惠州分公司
华宇互联网科技
互联网科技公司电话
未来的互联网科技
互联网女皇 腾讯科技
华脉科技工业互联网产品
互联网实体化科技化
中国科技互联网公司收入
剑昆互联网络科技有限公司临桂
南山科技园互联网公司有哪些
湖南诚度互联网科技有限公司
栖莱科技互联网股份有限公司
互联网还是高科技吗
互联网科技革命什么开始
世界互联网领先科技成果获奖
广州千川互联网科技有限公司
陕西云淼互联网科技公司
互联网科技创新方面的资料
高科技和互联网的现代国家
广州万易互联网科技有限公司
互联网科技集团
互联网 的科技创新
广西互联网科技公司
成都仁贤领冠互联网科技有限公司
新华互联网科技2019
互联网科技公司有风险吗
富国互联网科技002692
上海乐五互联网科技有限公司
信承诺互联网科技有限公司