IOOperatorHelper.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using static StackExchange.Redis.Role;
  2. namespace OASystem.API.OAMethodLib
  3. {
  4. public class IOOperatorHelper
  5. {
  6. /// <summary>
  7. /// 写文件(覆盖)
  8. /// </summary>
  9. /// <param name="content"></param>
  10. /// <param name="dir"></param>
  11. /// <param name="fileName"></param>
  12. /// <returns></returns>
  13. public string Write_CoverFile(string content, string dir, string fileName)
  14. {
  15. if (!System.IO.Directory.Exists(dir)) System.IO.Directory.CreateDirectory(dir); //该路径不存在时,在当前文件目录下创建文件夹"导出.."
  16. //不存在该文件时先创建
  17. String filePath = dir + fileName;
  18. System.IO.StreamWriter file1 = new System.IO.StreamWriter(filePath, false); //文件已覆盖方式添加内容
  19. file1.Write(content); //保存数据到文件
  20. file1.Close(); //关闭文件
  21. file1.Dispose(); //释放对象
  22. return filePath;
  23. }
  24. public string Read(string filePath)
  25. {
  26. string result = string.Empty;
  27. try
  28. {
  29. // 读取文本文件
  30. using (StreamReader sr = new StreamReader(filePath))
  31. {
  32. string line;
  33. // ReadLine()一行一行的循环读取
  34. //当然可以直接ReadToEnd()读到最后
  35. while ((line = sr.ReadLine()) != null)
  36. {
  37. result += line;
  38. }
  39. }
  40. }
  41. catch (Exception e)
  42. {
  43. }
  44. result = JsonConvert.DeserializeObject<string>(result);
  45. return result;
  46. }
  47. /// <summary>
  48. /// 移动文件
  49. /// </summary>
  50. /// <param name="sourceFilePath"></param>
  51. /// <param name="RecycleBinPath"></param>
  52. /// <returns></returns>
  53. public void MoveFile(string sourceFileFullPath, string RecycleBinPath)
  54. {
  55. //移动文件
  56. if (!string.IsNullOrEmpty(sourceFileFullPath) && System.IO.File.Exists(sourceFileFullPath))
  57. {
  58. string fileName = Path.GetFileName(sourceFileFullPath);
  59. System.IO.File.Move(sourceFileFullPath, RecycleBinPath + fileName);
  60. }
  61. }
  62. }
  63. }