CNAS取数仪器端升级
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

146 řádky
5.1KB

  1. using CnasSynchronousCommon;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Xml;
  8. using System.Xml.Serialization;
  9. namespace CnasSynchronousCommon
  10. {
  11. public static class FileHelper
  12. {
  13. /// <summary>
  14. /// 获取本地文件
  15. /// </summary>
  16. /// <param name="strPath"></param>
  17. public static string GetLocalFile(string strLocalPath,string strFileName)
  18. {
  19. string strLocalXML = "";
  20. try
  21. {
  22. string strUrl = strLocalPath + strFileName;
  23. if (File.Exists(strUrl))
  24. {
  25. string temp = "";
  26. using (StreamReader sr = new StreamReader(strUrl, Encoding.UTF8))
  27. {
  28. while (!sr.EndOfStream)//读到结尾退出
  29. {
  30. temp += sr.ReadLine();
  31. }
  32. }
  33. strLocalXML = temp;
  34. }
  35. else
  36. {
  37. AppLog.Error($"没有找到{strFileName}相关数据");
  38. }
  39. }
  40. catch (Exception ex)
  41. {
  42. AppLog.Error($"读取文件{strFileName}时发生错误:{ex.Message}");
  43. }
  44. return strLocalXML;
  45. }
  46. /// <summary>
  47. /// 保存本地文件
  48. /// </summary>
  49. /// <param name="strPath"></param>
  50. public static bool SaveLocalFile(string strLocalPath,string strFileName, string strData)
  51. {
  52. bool bIfSuccess = true;
  53. try
  54. {
  55. if (!Directory.Exists(strLocalPath))
  56. {
  57. Directory.CreateDirectory(strLocalPath);
  58. }
  59. string strUrl = strLocalPath + strFileName;
  60. FileStream fs = new FileStream(strUrl, FileMode.Create, FileAccess.ReadWrite);
  61. StringBuilder sBld = new StringBuilder();
  62. sBld.Append(strData);
  63. //byte[] byteFile = Encoding.Default.GetBytes(sBld.ToString());
  64. byte[] byteFile = Encoding.GetEncoding("utf-8").GetBytes(sBld.ToString());
  65. //参数:要写入到文件的数据数组,从数组的第几个开始写,一共写多少个字节
  66. fs.Write(byteFile, 0, byteFile.Length);
  67. fs.Close();
  68. fs.Dispose();
  69. }
  70. catch (Exception ex)
  71. {
  72. bIfSuccess = false;
  73. AppLog.Error($"保存文件{strFileName}时发生错误:{ex.Message}");
  74. }
  75. return bIfSuccess;
  76. }
  77. /// <summary>
  78. /// 获取当前dll所在目录
  79. /// </summary>
  80. /// <returns></returns>
  81. public static string getBasePath()
  82. {
  83. //获取当前DLL 所在路径
  84. string dllpath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
  85. if (dllpath != null && dllpath.Length > 0) dllpath = dllpath.Substring(8) + @"\";
  86. FileInfo tmpDllInfo = new FileInfo(dllpath);
  87. dllpath = tmpDllInfo.DirectoryName;
  88. dllpath = dllpath.Replace(tmpDllInfo.Directory.Name, "");
  89. return dllpath;
  90. }
  91. /// <summary>
  92. /// 将自定义对象序列化为XML字符串
  93. /// </summary>
  94. /// <param name="myObject">自定义对象实体</param>
  95. /// <returns>序列化后的XML字符串</returns>
  96. public static string SerializeToXml<T>(T myObject)
  97. {
  98. if (myObject != null)
  99. {
  100. XmlSerializer xs = new XmlSerializer(typeof(T));
  101. MemoryStream stream = new MemoryStream();
  102. XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
  103. writer.Formatting = Formatting.None;//缩进
  104. xs.Serialize(writer, myObject);
  105. stream.Position = 0;
  106. StringBuilder sb = new StringBuilder();
  107. using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
  108. {
  109. string line;
  110. while ((line = reader.ReadLine()) != null)
  111. {
  112. sb.Append(line);
  113. }
  114. reader.Close();
  115. }
  116. writer.Close();
  117. return sb.ToString();
  118. }
  119. return string.Empty;
  120. }
  121. /// <summary>
  122. /// 将XML字符串反序列化为对象
  123. /// </summary>
  124. /// <typeparam name="T">对象类型</typeparam>
  125. /// <param name="xml">XML字符</param>
  126. /// <returns></returns>
  127. public static T DeserializeToObject<T>(string xml)
  128. {
  129. T myObject;
  130. XmlSerializer serializer = new XmlSerializer(typeof(T));
  131. StringReader reader = new StringReader(xml);
  132. myObject = (T) serializer.Deserialize(reader);
  133. reader.Close();
  134. return myObject;
  135. }
  136. }
  137. }