using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace JinYuan.Helper { public class FileHelper { #region 1.文本文件读写 public static void WriteToTxt(string path, string content, bool isAppend = false) { //【1】创建文件流 FileStream fileStream = new FileStream(path, isAppend ? FileMode.Append : FileMode.Create); //【2】创建写入器 StreamWriter streamWriter = new StreamWriter(fileStream); //【3】以流的形式写入数据 streamWriter.Write(content); //【4】关闭写入器 streamWriter.Close(); //【5】关闭文件流 fileStream.Close(); } public static string ReadFromTxt(string path) { //判断文件是否存在 if (!File.Exists(path)) { return string.Empty; } //【1】创建文件流 FileStream fileStream = new FileStream(path, FileMode.Open); //【2】创建读取器 StreamReader streamReader = new StreamReader(fileStream); //【3】以流的方式读取 string content = streamReader.ReadToEnd(); //【4】关闭读取器 streamReader.Close(); //【5】关闭文件流 fileStream.Close(); return content; } #endregion #region 2.对象序列化文件 /// /// 序列化一个对象到一个文件中 /// /// /// public static void SerializeObject(object obj, string path) { FileStream fileStream = null; try { fileStream = new FileStream(path, FileMode.Create); BinaryFormatter binaryFormatter = new BinaryFormatter(); binaryFormatter.Serialize(fileStream, obj); } catch (Exception ex) { throw new Exception("序列化对象出错:" + ex.Message); } finally { fileStream.Close(); } } public static T DeSerializeObject(string path) { FileStream fileStream = null; try { fileStream = new FileStream(path, FileMode.Open); BinaryFormatter binaryFormatter = new BinaryFormatter(); return (T)binaryFormatter.Deserialize(fileStream); } catch (Exception ex) { throw new Exception("反序列化对象出错:" + ex.Message); } finally { fileStream.Close(); } } #endregion #region 3.对象序列化字符串 /// /// 将对象序列化成字符串 /// /// /// public static string SerializeObjToString(object obj) { IFormatter formatter = new BinaryFormatter(); string result = string.Empty; using (MemoryStream stream = new MemoryStream()) { formatter.Serialize(stream, obj); byte[] bytes = stream.ToArray(); result = Convert.ToBase64String(bytes); stream.Flush(); } return result; } /// /// 将字符串反序列化成对象 /// /// /// /// public static T DeSerializeObjFromString(string str) where T : class { IFormatter formatter = new BinaryFormatter(); byte[] bytes = Convert.FromBase64String(str); T obj = null; using (MemoryStream stream = new MemoryStream(bytes, 0, bytes.Length)) { obj = (T)formatter.Deserialize(stream); } return obj; } #endregion #region 4.文件的基本操作 /// /// 文件复制 /// /// /// public static void CopyFile(string srcFileName, string desFileName) { if (File.Exists(desFileName)) { File.Delete(desFileName); } File.Copy(srcFileName, desFileName); } /// /// 文件移动 /// /// /// public static void MoveFile(string srcFileName, string desFileName) { if (File.Exists(srcFileName)) { if (File.Exists(desFileName)) { File.Delete(desFileName); } File.Move(srcFileName, desFileName); } } /// /// 文件删除 /// /// public static void DeleteFile(string fileName) { if (File.Exists(fileName)) { File.Delete(fileName); } } #endregion #region 5.文件夹的相关操作 /// /// 获取指定目录下的所有文件 /// /// /// public static string[] GetFiles(string path) { return Directory.GetFiles(path); } /// /// 获取指定目录下的所有子目录 /// /// /// public static string[] GetDirectories(string path) { return Directory.GetDirectories(path); } /// /// 创建文件夹 /// /// public static void CreateDirectory(string path) { Directory.CreateDirectory(path); } /// /// 删除指定目录下的所有子目录和文件 /// /// public static void DeleteFiles(string path) { DirectoryInfo directory = new DirectoryInfo(path); directory.Delete(true); } #endregion } }