做.NET后台开发的同学,对缓存处理一定不陌生,这里把我混迹C#圈子十余载珍藏的基础类库分享出来,希望能够给刚踏入开发门槛的朋友一些帮助。
后续我会逐步分享基础库的其余部分,先列个大纲:
C#个人珍藏基础类库分享 — 1、通用缓存帮助类CacheHelper
C#个人珍藏基础类库分享 — 2、Memcached缓存帮助类MemcachedHelper
C#个人珍藏基础类库分享 — 3、目录、文件帮助类FileHelper
C#个人珍藏基础类库分享 — 4、字节数组帮助类BytesObjectHelper
C#个人珍藏基础类库分享 — 5、日志帮助类LogHelper
C#个人珍藏基础类库分享 — 6、数据库处理帮助类SqlHelper
C#个人珍藏基础类库分享 — 7、Xml处理帮助类XmlHelper
C#个人珍藏基础类库分享 — 8、通用工具帮助类ToolHelper
直接进入主题,文件处理类主要包括以下方法:
1、获得指定(文件或目录)相对路径的物理路径
2、确保目录存在,如果目录不存在,则创建目录(包括上级目录)
3、确保文件存在,如果文件目录不存在,则创建目录(包括上级目录)
4、将默认编码类型(Unicode)的字符串,追加至指定文件
5、将指定编码类型的字符串,追加至指定文件
6、将默认编码类型(Unicode)的字符串,写入指定文件
7、将指定编码类型的字符串,写入指定文件
8、依据默认编码类型(Unicode),获取指定文件、指定范围的字符
9、依据指定的编码类型,获取指定文件、指定范围的字符
10、基于默认编码类型(Unicode),将字节数组追加至指定的二进制文件
11、基于所提供编码类型,将字节数组追加至指定的二进制文件
12、基于默认编码类型(Unicode),将字节数组写入指定的二进制文件
13、基于所提供编码类型,将字节数组写入指定的二进制文件
14、依据默认编码类型(Unicode),获取指定文件、指定范围的二进制数据
15、依据指定的编码类型,获取指定文件、指定范围的二进制数据
源码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using Microsoft.Win32.SafeHandles;namespace BaseUtilities
{/// /// 目录、文件与文件内容相关,常用的处理方法集合。/// public class FileHelper{#region Path & Directory & File/// /// 获得指定(文件或目录)相对路径的物理路径。/// 支持 Web 程序、Windows 服务程序、控制台等程序。 /// /// 相对路径/// 返回指定相对路径的物理路径(异常时返回值为 Null)。 public static string GetMapPath(string path){try{if (HttpContext.Current != null)return HttpContext.Current.Server.MapPath(path);elsereturn Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);}catch (Exception ex){LogHelper.Error("Sxmobi.FileHelper", null, ex);return null;}}/// /// 确保目录存在。/// 如果目录不存在,则创建目录(包括上级目录)。 /// /// 目录路径(不含文件名)/// 返回一个 Boolean 值,如果目录不存在且创建目录出现异常时,返回值为 False。 public static bool EnsureDir(string path){try{if (Directory.Exists(path))return true;if (EnsureDir(Directory.GetParent(path).ToString())){Directory.CreateDirectory(path);return true;}return false;}catch (Exception ex){LogHelper.Error("Sxmobi.FileHelper", null, ex);return false;}}/// /// 确保文件存在。/// 如果文件目录不存在,则创建目录(包括上级目录)。 /// /// 文件路径/// 返回一个 Boolean 值,如果目录或文件不存在且创建它们出现异常时,返回值为 False。 public static bool EnsureFile(string path){try{if (File.Exists(path))return true;if (EnsureDir(Directory.GetParent(path).ToString())){File.Create(path).Close();return true;}return false;}catch (Exception ex){LogHelper.Error("Sxmobi.FileHelper", null, ex);return false;}}#endregion#region Text 相关/// /// 将默认编码类型(Unicode)的字符串,追加至指定文件。/// 系统推荐使用默认的 Unicode 编码类型格式来进行文字的读取与写入。 /// 方法将确保目录与文件存在 /// /// 文件路径/// 需要写入的字符/// 指定是否将字符串写入新行/// 返回一个 Boolean 值,如果指定的目录、文件不存在且创建它们(或写入字符时)出现异常,返回值为 False。 public static bool AddFileText(string path, string text, bool isNewLine){return AddFileText(path, Encoding.Unicode, text, isNewLine);}/// /// 将指定编码类型的字符串,追加至指定文件。/// 方法将确保目录与文件存在。 /// 系统推荐使用默认的 Unicode 编码类型格式来进行文字的读取与写入。 /// /// 文件路径/// 编码类型/// 需要写入的字符/// 指定是否将字符串写入新行/// 返回一个 Boolean 值,如果指定的目录、文件不存在且创建它们(或写入字符时)出现异常,返回值为 False。 public static bool AddFileText(string path, Encoding encodingType, string text, bool isNewLine){if (!EnsureFile(path))return false;try{using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)){using (StreamWriter sw = new StreamWriter(fs, encodingType)){if (isNewLine)sw.WriteLine(text);elsesw.Write(text);}}return true;}catch (Exception ex){LogHelper.Error("Sxmobi.FileHelper", null, ex);return false;}}/// /// 将默认编码类型(Unicode)的字符串,写入指定文件。/// 方法将确保目录与文件存在。 /// 系统推荐使用默认的 Unicode 编码类型格式来进行文字的读取与写入。 /// /// 文件路径/// 需要写入的字符/// 返回一个 Boolean 值,如果指定的目录、文件不存在且创建它们(或写入字符时)出现异常,返回值为 False。 public static bool SetFileText(string path, string text){return SetFileText(path, Encoding.Unicode, text);}/// /// 将指定编码类型的字符串,写入指定文件。/// 方法将确保目录与文件存在。 /// 系统推荐使用默认的 Unicode 编码类型格式来进行文字的读取与写入。 /// /// 文件路径/// 编码类型/// 需要写入的字符/// 返回一个 Boolean 值,如果指定的目录、文件不存在且创建它们(或写入字符时)出现异常,返回值为 False。 public static bool SetFileText(string path, Encoding encodingType, string text){if (!EnsureDir(Directory.GetParent(path).ToString()))return false;try{using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)){using (StreamWriter sw = new StreamWriter(fs, encodingType)){sw.Write(text);}}return true;}catch (Exception ex){LogHelper.Error("Sxmobi.FileHelper", null, ex);return false;}}/// /// 依据默认编码类型(Unicode),获取指定文件、指定范围的字符。/// 系统推荐使用默认的 Unicode 编码类型格式来进行文字的读取与写入。 /// /// 文件路径/// 指定要获取内容的起始索引位置(例如文件内容:“类型 FileHelper 是...”,如果 index 设置为 3,则取得字符串“FileHelper 是...”)/// 指定要获取内容的长度,内容读取长度设置不宜过大,以避免内存溢出/// 返回 String 类型值,当文件不存在或其它异常时,返回值为 Null。 public static string GetFileText(string path, int index, int count){return GetFileText(path, Encoding.Unicode, index, count);}/// /// 依据指定的编码类型,获取指定文件、指定范围的字符。/// 系统推荐使用默认的 Unicode 编码类型格式来进行文字的读取与写入。 /// /// 文件路径/// 编码类型/// 指定要获取内容的起始索引位置(例如文件内容:“类型 FileHelper 是...”,如果 index 设置为 3,则取得字符串“FileHelper 是...”)/// 指定要获取内容的长度,内容读取长度设置不宜过大,以避免内存溢出/// 返回 String 类型值,当文件不存在或其它异常时,返回值为 Null。 public static string GetFileText(string path, Encoding encodingType, int index, int count){try{if (!File.Exists(path))return null;int maxReads = 10000;char[] tempChars = new char[maxReads];char[] chars = new char[count];int loopCount = index / maxReads;int tempIndex = index % maxReads;using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)){using (StreamReader sr = new StreamReader(fs, encodingType)){// 如果起始索引超过 1W 个字符,则以 1W 个字符为单位,循环丢弃起始索引之前的所有字符,以避免内存开销过大。while (loopCount-- > 0)sr.ReadBlock(tempChars, 0, maxReads);sr.ReadBlock(tempChars, 0, tempIndex);sr.ReadBlock(chars, 0, count);}}return new string(chars);}catch (Exception ex){LogHelper.Error("Sxmobi.FileHelper", null, ex);return null;}}#endregion#region Binary 相关/// /// 基于默认编码类型(Unicode),将字节数组追加至指定的二进制文件/// /// 二进制文件/// 字节数组/// 返回一个 Boolean 值,如果指定的目录、文件不存在且创建它们(或写入二进制数据时)出现异常,返回值为 False。 public static bool AddFileBinary(string path, byte[] bytes){return AddFileBinary(path, Encoding.Unicode, bytes);}/// /// 基于所提供编码类型,将字节数组追加至指定的二进制文件/// /// 二进制文件/// 编码类型/// 字节数组/// 返回一个 Boolean 值,如果指定的目录、文件不存在且创建它们(或写入二进制数据时)出现异常,返回值为 False。 public static bool AddFileBinary(string path, Encoding encodingType, byte[] bytes){if (!EnsureDir(Directory.GetParent(path).ToString()))return false;try{using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)){using (BinaryWriter bw = new BinaryWriter(fs, encodingType)){bw.Write(bytes);}}return true;}catch (Exception ex){LogHelper.Error("Sxmobi.FileHelper", null, ex);return false;}}/// /// 基于默认编码类型(Unicode),将字节数组写入指定的二进制文件/// /// 二进制文件/// 字节数组/// 返回一个 Boolean 值,如果指定的目录、文件不存在且创建它们(或写入二进制数据时)出现异常,返回值为 False。 public static bool SetFileBinary(string path, byte[] bytes){return SetFileBinary(path, Encoding.Unicode, bytes);}/// /// 基于所提供编码类型,将字节数组写入指定的二进制文件/// /// 二进制文件/// 编码类型/// 字节数组/// 返回一个 Boolean 值,如果指定的目录、文件不存在且创建它们(或写入二进制数据时)出现异常,返回值为 False。 public static bool SetFileBinary(string path, Encoding encodingType, byte[] bytes){if (!EnsureDir(Directory.GetParent(path).ToString()))return false;try{using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)){using (BinaryWriter bw = new BinaryWriter(fs, encodingType)){bw.Write(bytes);}}return true;}catch (Exception ex){LogHelper.Error("Sxmobi.FileHelper", null, ex);return false;}}/// /// 依据默认编码类型(Unicode),获取指定文件、指定范围的二进制数据。/// /// 文件路径/// 指定要获取数据的起始索引位置/// 指定要获取数据的长度,内容读取长度设置不宜过大,以避免内存溢出/// 返回 byte[] 类型值,当文件不存在或其它异常时,返回值为 Null。 public static byte[] GetFileBinary(string path, int index, int count){return GetFileBinary(path, Encoding.Unicode, index, count);}/// /// 依据指定的编码类型,获取指定文件、指定范围的二进制数据。/// /// 文件路径/// 编码类型/// 指定要获取数据的起始索引位置/// 指定要获取数据的长度,内容读取长度设置不宜过大,以避免内存溢出/// 返回 byte[] 类型值,当文件不存在或其它异常时,返回值为 Null。 public static byte[] GetFileBinary(string path, Encoding encodingType, int index, int count){try{if (!File.Exists(path))return null;int maxReads = 1024000;byte[] bytes = new byte[count];int loopCount = index / maxReads;int tempIndex = index % maxReads;using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)){using (BinaryReader br = new BinaryReader(fs, encodingType)){// 如果起始索引超过1M(1000K),则以1M为单位,循环丢弃起始索引之前的所有字节,以避免内存开销过大。while (loopCount-- > 0)br.ReadBytes(maxReads);br.ReadBytes(tempIndex);bytes = br.ReadBytes(count);}}return bytes;}catch (Exception ex){LogHelper.Error("Sxmobi.FileHelper", null, ex);return null;}}#endregion}
}