CsvDirectoryConfigMassage.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. namespace ToolsClassLibrary
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public static class CsvDirectoryConfigMassage
  10. {
  11. /// <summary>
  12. /// 将指定目录下CSV文件进行读取,格式List<文件名,List<测点编号>>
  13. /// </summary>
  14. /// <param name="path">装载CSV文件的目录</param>
  15. /// <param name="errString">错误描述</param>
  16. /// <returns></returns>
  17. public static Dictionary<string, List<string>> GetCsvAndId(string path, out string errString)
  18. {
  19. Dictionary<string, List<string>> config = new Dictionary<string, List<string>>();
  20. errString = "";
  21. try
  22. {
  23. string dir = path;
  24. if (Directory.Exists(dir) == false)//如果不存在就创建file文件夹
  25. {
  26. Console.WriteLine("缺失配置文件夹csvfiles。");
  27. errString = "缺失配置文件夹csvfiles。";
  28. }
  29. string[] files = Directory.GetFiles(dir);
  30. foreach (string file in files)
  31. {
  32. if (file.LastIndexOf(".csv") > -1)
  33. {
  34. CsvStreamReader Cr = new CsvStreamReader(file);
  35. if (Cr.ColCount >= 1 && Cr.RowCount > 0)
  36. {
  37. config.Add(file, new List<string>());
  38. for (int i = 1; i <= Cr.RowCount; i++)
  39. {
  40. //Console.WriteLine(Cr.ColCount);
  41. config[file].Add(Cr[i, 1]);
  42. //config[file].Add(Cr[i, 2]);
  43. //config[file].Add(Cr[i, 3]);
  44. }
  45. }
  46. }
  47. }
  48. }
  49. catch (Exception e)
  50. {
  51. Console.WriteLine(e.Message.ToString());
  52. errString = e.Message.ToString();
  53. }
  54. return config;
  55. }
  56. }
  57. }