You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.8 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Configuration;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Xml;
  10. namespace Euro.Transfer.Base
  11. {
  12. /// <summary>
  13. /// 工具类
  14. /// </summary>
  15. public class ServiceTools : ServiceBase, IConfigurationSectionHandler
  16. {
  17. /// <summary>
  18. /// 获取configSections节点
  19. /// </summary>
  20. /// <returns></returns>
  21. public static XmlNode GetConfigSections()
  22. {
  23. var doc = new XmlDocument();
  24. doc.Load(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath);
  25. return doc.DocumentElement.FirstChild;
  26. }
  27. /// <summary>
  28. /// 获取section节点
  29. /// </summary>
  30. /// <param name="nodeName"></param>
  31. /// <returns></returns>
  32. public static NameValueCollection GetSection(string nodeName)
  33. {
  34. return (NameValueCollection)ConfigurationManager.GetSection(nodeName);
  35. }
  36. /// <summary>
  37. /// 停止Windows服务
  38. /// </summary>
  39. /// <param name="serviceName">服务名称</param>
  40. public static void WindowsServiceStop(string serviceName)
  41. {
  42. var control = new System.ServiceProcess.ServiceController(serviceName);
  43. control.Stop();
  44. control.Dispose();
  45. }
  46. /// <summary>
  47. /// 写日志
  48. /// </summary>
  49. /// <param name="path">日志文件</param>
  50. /// <param name="cont">日志内容</param>
  51. /// <param name="isAppend">是否追加方式</param>
  52. /// <param name="foder">todo: describe foder parameter on WriteWordLog</param>
  53. public static void WriteWordLog(string path, string cont, bool isAppend, string foder)
  54. {
  55. if (path == "")
  56. {
  57. path = System.Windows.Forms.Application.StartupPath.ToString() + @"\WordLogs\" + foder + "\\";
  58. }
  59. if (!Directory.Exists(path))
  60. {
  61. Directory.CreateDirectory(path);
  62. }
  63. path += DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
  64. using (StreamWriter sw = new StreamWriter(path, isAppend, Encoding.UTF8))
  65. {
  66. sw.WriteLine(DateTime.Now);
  67. sw.WriteLine(cont);
  68. sw.WriteLine("");
  69. sw.Close();
  70. }
  71. }
  72. /// <summary>
  73. /// 写錯誤日志
  74. /// </summary>
  75. /// <param name="path">日志文件</param>
  76. /// <param name="cont">日志内容</param>
  77. /// <param name="isAppend">是否追加方式</param>
  78. public static void WriteLog(string path, string cont, bool isAppend)
  79. {
  80. if (path == "")
  81. {
  82. path = System.Windows.Forms.Application.StartupPath.ToString() + @"\ErrorLogs\";
  83. }
  84. if (!Directory.Exists(path))
  85. {
  86. Directory.CreateDirectory(path);
  87. }
  88. path += "//" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
  89. using (StreamWriter sw = new StreamWriter(path, isAppend, Encoding.UTF8))
  90. {
  91. sw.WriteLine(DateTime.Now);
  92. sw.WriteLine(cont);
  93. sw.WriteLine("");
  94. sw.Close();
  95. }
  96. }
  97. /// <summary>
  98. /// 写文字檔
  99. /// </summary>
  100. /// <param name="cont">文字檔内容</param>
  101. /// <param name="subname">文字檔副檔名</param>
  102. /// <param name="prename">文字檔前最</param>
  103. /// <param name="orgid">todo: describe orgid parameter on WriteWords</param>
  104. public static void WriteWords(string cont, string orgid, string subname, string prename = "")
  105. {
  106. var path = Common.ConfigGetValue("", "WriteWordPath") + "\\";
  107. if (!Directory.Exists(path))
  108. {
  109. Directory.CreateDirectory(path);
  110. }
  111. var sTransferID = Common.ConfigGetValue("", "TransferID");
  112. var name = Common.GetSystemSetting(orgid, sTransferID);
  113. path += prename + name + subname;
  114. using (StreamWriter sw = new StreamWriter(path, true, Encoding.Default))
  115. {
  116. sw.WriteLine(cont);
  117. sw.Close();
  118. }
  119. }
  120. /// <summary>
  121. /// 实现接口以读写app.config
  122. /// </summary>
  123. /// <param name="parent"></param>
  124. /// <param name="configContext"></param>
  125. /// <param name="section"></param>
  126. /// <returns></returns>
  127. public object Create(object parent, object configContext, XmlNode section)
  128. {
  129. var handler = new NameValueSectionHandler();
  130. return handler.Create(parent, configContext, section);
  131. }
  132. }
  133. }