请选择 进入手机版 | 继续访问电脑版

马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#中对文件File常用操作方法的工具类

  [复制链接]
查看2279 | 回复8 | 2019-11-8 09:57:47 | 显示全部楼层 |阅读模式

场景

C#中File类的常用读取与写入文件方法的利用:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99693983

注:

博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的步伐猿
获取编程相干电子书、教程推送与免费下载。

实现

获取文件的扩展名

  1. /// <summary>
  2. /// 获取文件的扩展名
  3. /// </summary>
  4. /// <param name="filename">完备文件名</param>
  5. /// <returns>返回扩展名</returns>
  6. public static string GetPostfixStr(string filename)
  7. {
  8. int num = filename.LastIndexOf(".");
  9. int length = filename.Length;
  10. return filename.Substring(num, length - num);
  11. }
复制代码

读取文件内容

  1. /// <summary>
  2. /// 读取文件内容
  3. /// </summary>
  4. /// <param name="path">要读取的文件路径</param>
  5. /// <returns>返回文件内容</returns>
  6. public static string ReadFile(string path)
  7. {
  8. string result;
  9. if (!System.IO.File.Exists(path))
  10. {
  11. result = "不存在相应的目次";
  12. }
  13. else
  14. {
  15. System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
  16. System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.Default);
  17. result = streamReader.ReadToEnd();
  18. streamReader.Close();
  19. streamReader.Dispose();
  20. }
  21. return result;
  22. }
复制代码

指定编码格式读取文件内容

  1. /// <summary>
  2. /// 读取文件内容
  3. /// </summary>
  4. /// <param name="path">要读取的文件路径</param>
  5. /// <param name="encoding">编码格式</param>
  6. /// <returns>返回文件内容</returns>
  7. public static string ReadFile(string path, System.Text.Encoding encoding)
  8. {
  9. string result;
  10. if (!System.IO.File.Exists(path))
  11. {
  12. result = "不存在相应的目次";
  13. }
  14. else
  15. {
  16. System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
  17. System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, encoding);
  18. result = streamReader.ReadToEnd();
  19. streamReader.Close();
  20. streamReader.Dispose();
  21. }
  22. return result;
  23. }
复制代码

向指定文件写入内容

  1. /// <summary>
  2. /// 向指定文件写入内容
  3. /// </summary>
  4. /// <param name="path">要写入内容的文件完备路径</param>
  5. /// <param name="content">要写入的内容</param>
  6. public static void WriteFile(string path, string content)
  7. {
  8. try
  9. {
  10. object obj = new object();
  11. if (!System.IO.File.Exists(path))
  12. {
  13. System.IO.FileStream fileStream = System.IO.File.Create(path);
  14. fileStream.Close();
  15. }
  16. lock (obj)
  17. {
  18. using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(path, false, System.Text.Encoding.Default))
  19. {
  20. streamWriter.WriteLine(content);
  21. streamWriter.Close();
  22. streamWriter.Dispose();
  23. }
  24. }
  25. }
  26. catch (System.Exception ex)
  27. {
  28. ICSharpCode.Core.LoggingService<FileHelper>.Error(String.Format("写入文件{0}非常:{1}", path, ex.Message), ex);
  29. }
  30. }
复制代码

指定编码格式向文件写入内容

  1. /// <summary>
  2. /// 向指定文件写入内容
  3. /// </summary>
  4. /// <param name="path">要写入内容的文件完备路径</param>
  5. /// <param name="content">要写入的内容</param>
  6. /// <param name="encoding">编码格式</param>
  7. public static void WriteFile(string path, string content, System.Text.Encoding encoding)
  8. {
  9. try
  10. {
  11. object obj = new object();
  12. if (!System.IO.File.Exists(path))
  13. {
  14. System.IO.FileStream fileStream = System.IO.File.Create(path);
  15. fileStream.Close();
  16. }
  17. lock (obj)
  18. {
  19. using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(path, false, encoding))
  20. {
  21. streamWriter.WriteLine(content);
  22. streamWriter.Close();
  23. streamWriter.Dispose();
  24. }
  25. }
  26. }
  27. catch (System.Exception ex)
  28. {
  29. ICSharpCode.Core.LoggingService<FileHelper>.Error(String.Format("写入文件{0}非常:{1}", path, ex.Message), ex);
  30. }
  31. }
复制代码

文件复制

  1. /// <summary>
  2. /// 文件复制
  3. /// </summary>
  4. /// <param name="orignFile">源文件完备路径</param>
  5. /// <param name="newFile">目标文件完备路径</param>
  6. public static void FileCoppy(string orignFile, string newFile)
  7. {
  8. System.IO.File.Copy(orignFile, newFile, true);
  9. }
复制代码

文件删除

  1. /// <summary>
  2. /// 删除文件
  3. /// </summary>
  4. /// <param name="path">要删除的文件的完备路径</param>
  5. public static void FileDel(string path)
  6. {
  7. System.IO.File.Delete(path);
  8. }
复制代码

文件移动

  1. /// <summary>
  2. /// 文件移动(剪贴->粘贴)
  3. /// </summary>
  4. /// <param name="orignFile">源文件的完备路径</param>
  5. /// <param name="newFile">目标文件完备路径</param>
  6. public static void FileMove(string orignFile, string newFile)
  7. {
  8. System.IO.File.Move(orignFile, newFile);
  9. }
复制代码

判定一组文件是否都存在

  1. /// <summary>
  2. /// 判定一组文件是否都存在
  3. /// </summary>
  4. /// <param name="filePathList">文件路径List</param>
  5. /// <returns>文件是否全部存在</returns>
  6. public static bool IsFilesExist(List<string> filePathList)
  7. {
  8. bool isAllExist = true;
  9. foreach(string filePath in filePathList)
  10. {
  11. if(!File.Exists(filePath))
  12. {
  13. isAllExist = false;
  14. }
  15. }
  16. return isAllExist;
  17. }
复制代码

创建目次

  1. /// <summary>
  2. /// 创建目次
  3. /// </summary>
  4. /// <param name="orignFolder">当前目次</param>
  5. /// <param name="newFloder">要创建的目次名</param>
  6. public static void FolderCreate(string orignFolder, string newFloder)
  7. {
  8. System.IO.Directory.SetCurrentDirectory(orignFolder);
  9. System.IO.Directory.CreateDirectory(newFloder);
  10. }
复制代码

删除目次

  1. /// <summary>
  2. /// 删除目次
  3. /// </summary>
  4. /// <param name="dir">要删除的目次</param>
  5. public static void DeleteFolder(string dir)
  6. {
  7. if (System.IO.Directory.Exists(dir))
  8. {
  9. string[] fileSystemEntries = System.IO.Directory.GetFileSystemEntries(dir);
  10. for (int i = 0; i < fileSystemEntries.Length; i++)
  11. {
  12. string text = fileSystemEntries[i];
  13. if (System.IO.File.Exists(text))
  14. {
  15. System.IO.File.Delete(text);
  16. }
  17. else
  18. {
  19. FileHelper.DeleteFolder(text);
  20. }
  21. }
  22. System.IO.Directory.Delete(dir);
  23. }
  24. }
复制代码

目次内容复制

  1. /// <summary>
  2. /// 目次内容复制
  3. /// </summary>
  4. /// <param name="srcPath">源目次</param>
  5. /// <param name="aimPath">目标目次</param>
  6. public static void CopyDir(string srcPath, string aimPath)
  7. {
  8. try
  9. {
  10. if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
  11. {
  12. aimPath += System.IO.Path.DirectorySeparatorChar;
  13. }
  14. if (!System.IO.Directory.Exists(aimPath))
  15. {
  16. System.IO.Directory.CreateDirectory(aimPath);
  17. }
  18. string[] fileSystemEntries = System.IO.Directory.GetFileSystemEntries(srcPath);
  19. string[] array = fileSystemEntries;
  20. for (int i = 0; i < array.Length; i++)
  21. {
  22. string text = array[i];
  23. if (System.IO.Directory.Exists(text))
  24. {
  25. FileHelper.CopyDir(text, aimPath + System.IO.Path.GetFileName(text));
  26. }
  27. else
  28. {
  29. System.IO.File.Copy(text, aimPath + System.IO.Path.GetFileName(text), true);
  30. }
  31. }
  32. }
  33. catch (System.Exception ex)
  34. {
  35. throw new System.Exception(ex.ToString());
  36. }
  37. }
复制代码

C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则