TDMSDataBuilder.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using TDMS.Common;
  5. using TDMS.Default;
  6. using TDMS.Externals;
  7. namespace TDMS
  8. {
  9. /// <summary>
  10. /// TDMS数据文件的创建类。技术上是工厂模式的创建模式。也是<see cref="NKnife.TDMS" />的入口类。
  11. /// </summary>
  12. public static class TDMSDataBuilder
  13. {
  14. private static readonly int s_waitTimeBeforeOpeningFile = 20; //防止文件正在被其他进程写入(保存)而导致Open失败
  15. static TDMSDataBuilder()
  16. {
  17. #if RELEASE
  18. s_waitTimeBeforeOpeningFile = 50; //生产环境下,等待时间略长一些,以确保成功率。
  19. #endif
  20. var libsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DDC.DLLDirectory);
  21. VerifyNiLibFiles(libsDirectory);
  22. }
  23. /// <summary>
  24. /// 检查dll库文件是否存在
  25. /// </summary>
  26. internal static void VerifyNiLibFiles(string libsDirectory)
  27. {
  28. string[] dllFiles = ["dacasr.dll", "nilibddc.dll", "tdms_ebd.dll"];
  29. foreach (var dllFile in dllFiles)
  30. {
  31. var filePath = Path.Combine(libsDirectory, dllFile);
  32. if(!File.Exists(filePath))
  33. {
  34. throw new FileNotFoundException($"NI TDMS library file does not exist:{filePath}");
  35. }
  36. }
  37. }
  38. public static ITDMSFile BuildNew(string filePath,
  39. string name,
  40. string description = "",
  41. string title = "",
  42. string author = "",
  43. string fileType = Constants.DDC_FILE_TYPE_TDM_STREAMING)
  44. {
  45. ITDMSFile tdmsFile = new TDMSFile();
  46. tdmsFile.Create(filePath, fileType, name, description, title, author);
  47. return tdmsFile;
  48. }
  49. /// <summary>
  50. /// 打开一个已经存在的TDMS文件<br />
  51. /// </summary>
  52. /// <param name="filePath">文件路径</param>
  53. /// <returns>返回一个文件的实例</returns>
  54. public static ITDMSFile OpenExisting(string filePath)
  55. {
  56. ITDMSFile tdmsFile = new TDMSFile();
  57. Thread.Sleep(s_waitTimeBeforeOpeningFile);
  58. tdmsFile.Open(filePath);
  59. return tdmsFile;
  60. }
  61. /// <summary>
  62. /// 打开一个已经存在的TDMS文件<br />
  63. /// </summary>
  64. /// <param name="fileInfo">文件信息</param>
  65. /// <returns>返回一个文件的实例</returns>
  66. public static ITDMSFile OpenExistingFile(TDMSFileInfo fileInfo)
  67. {
  68. ITDMSFile tdmsFile = new TDMSFile();
  69. Thread.Sleep(s_waitTimeBeforeOpeningFile);
  70. tdmsFile.Open(fileInfo);
  71. return tdmsFile;
  72. }
  73. }
  74. }