FileHelper.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Configuration;
  5. using System.IO;
  6. using System.Text.RegularExpressions;
  7. using DSOFile;
  8. namespace EMISOnline.WinService.Common
  9. {
  10. public class FileHelper
  11. {
  12. static object locker = new object();//创建锁
  13. public static string GetFilePropertiesComments(string filePath)
  14. {
  15. lock (locker)
  16. {
  17. DSOFile.OleDocumentPropertiesClass file = new OleDocumentPropertiesClass();
  18. file.Open(filePath, true, dsoFileOpenOptions.dsoOptionDefault);
  19. string Comments = file.SummaryProperties.Comments;
  20. file.Close(false);
  21. return Comments;
  22. }
  23. }
  24. public static void SaveFilePropertiesComments(string filePath, string Comments)
  25. {
  26. lock (locker)
  27. {
  28. DSOFile.OleDocumentPropertiesClass file = new OleDocumentPropertiesClass();
  29. file.Open(filePath, false, dsoFileOpenOptions.dsoOptionDefault);
  30. file.SummaryProperties.Comments = Comments;
  31. file.Save();
  32. file.Close(false);
  33. }
  34. }
  35. public static List<FileInfo> GetAllFiles(string dirPath, string FtpFileType)
  36. {
  37. List<FileInfo> rtn = new List<FileInfo>();
  38. DirectoryInfo folder = new DirectoryInfo(dirPath);
  39. //先查找本目录下的文件信息
  40. string[] arysFilter = FtpFileType.Split('|');
  41. foreach (string filter in arysFilter)
  42. {
  43. foreach (FileInfo info in folder.GetFiles(filter))
  44. {
  45. rtn.Add(info);
  46. }
  47. }
  48. //递归查找子目录文件
  49. foreach (DirectoryInfo d in folder.GetDirectories())
  50. {
  51. rtn.AddRange(GetAllFiles(d.FullName,FtpFileType));
  52. }
  53. return rtn;
  54. }
  55. public static List<string> ReadTxt(string filePath)
  56. {
  57. List<string> rtn = new List<string>();
  58. StreamReader sr = new StreamReader(filePath, Encoding.Default);
  59. String line;
  60. while ((line = sr.ReadLine()) != null)
  61. {
  62. string str = line.ToString();
  63. rtn.Add(str);
  64. }
  65. sr.Close();
  66. sr.Dispose();
  67. return rtn;
  68. }
  69. }
  70. }