Program.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.ServiceProcess;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Diagnostics;
  8. namespace XMLMatrixConvert
  9. {
  10. static class Program
  11. {
  12. static void Main()
  13. {
  14. if (Debugger.IsAttached)
  15. {
  16. Service1 ser1 = new Service1();
  17. Console.ReadLine();
  18. }
  19. else
  20. {
  21. ServiceBase[] ServicesToRun;
  22. ServicesToRun = new ServiceBase[]
  23. {
  24. new Service1()
  25. };
  26. ServiceBase.Run(ServicesToRun);
  27. }
  28. }
  29. }
  30. }

关键字:Dbugger.IsAttached
此处的作用的,调试状态下,作为调整。

Services1

  1. using System;
  2. using System.Data;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.ServiceProcess;
  6. using System.Timers;
  7. namespace TestNamespace
  8. {
  9. public partial class Service1 : ServiceBase
  10. {
  11. #region Variables
  12. private string _SourceSQLConnORC = string.Empty;
  13. private string _SourceSQLConnVRS = string.Empty;
  14. private Timer _Timer = new Timer();
  15. private int _TimerInterval = 0;
  16. private string _MappingFolderPath = string.Empty;
  17. #endregion
  18. public Service1()
  19. {
  20. ServiceInitial();
  21. InitializeComponent();
  22. }
  23. public void ServiceInitial()
  24. {
  25. GetConfig();
  26. GeneralFunctions.WriteLog.WriteLogFun("ServiceInitial: Get config end");
  27. _Timer.Interval = _TimerInterval * 1000 * 60; //30s
  28. if (Debugger.IsAttached)
  29. {
  30. _Timer.Interval = 5000;
  31. }
  32. _Timer.Elapsed += new ElapsedEventHandler(TimedEvent); //此处委托给TimeEvent
  33. _Timer.Enabled = true;
  34. _Timer.AutoReset = false;
  35. _Timer.Start();
  36. GeneralFunctions.WriteLog.WriteLogFun("ServiceInitial: Get config and Timer Started.");
  37. }
  38. private void GetConfig()
  39. {
  40. _SourceSQLConnORC = GeneralFunctions.ReadAndSetConfigFile.GetAppConfig("SourceSQLConnORC");
  41. _SourceSQLConnVRS = GeneralFunctions.ReadAndSetConfigFile.GetAppConfig("SourceSQLConnVRS");
  42. _MappingFolderPath = GeneralFunctions.ReadAndSetConfigFile.GetAppConfig("MappingFolderPath");
  43. if (Debugger.IsAttached || string.IsNullOrWhiteSpace(_MappingFolderPath))
  44. {
  45. _MappingFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "eTest");
  46. DirectoryInfo dinfo = new DirectoryInfo(_MappingFolderPath);
  47. dinfo.Create();
  48. GeneralFunctions.WriteLog.WriteLogFun("Create eTest folder.");
  49. }
  50. try
  51. {
  52. _TimerInterval = int.Parse(GeneralFunctions.ReadAndSetConfigFile.GetAppConfig("TimerInterval"));
  53. }
  54. catch
  55. {
  56. _TimerInterval = 30;
  57. }
  58. }
  59. public void TimedEvent(object source, ElapsedEventArgs e)
  60. {
  61. _Timer.Enabled = false;
  62. try
  63. {
  64. //get orc data
  65. DataTable dt_orc = DataBaseFunctions.GetORCData.GetORCDataFunc(_SourceSQLConnORC);
  66. //get Json data
  67. //DataView dataView = dt_orc.DefaultView;DataTable dataTableDistinct = dataView.ToTable(true, "PanelId");
  68. eMapFunctions.loadlAndUpdateXml.CreateAndUpdateXml(dt_orc, _MappingFolderPath, _SourceSQLConnVRS);
  69. }
  70. catch (Exception ex)
  71. {
  72. GeneralFunctions.WriteLog.WriteLogFun("Error:" + ex.Message);
  73. GeneralFunctions.WriteLog.WriteLogFun("StackTrace:" + ex.StackTrace);
  74. }
  75. finally
  76. {
  77. GeneralFunctions.WriteLog.WriteLogFun("Process End.");
  78. //next timer
  79. //_Timer.Enabled = true;
  80. _Timer.Enabled = false;
  81. }
  82. }
  83. protected override void OnStart(string[] args)
  84. {
  85. }
  86. protected override void OnStop()
  87. {
  88. }
  89. }
  90. }

App.config

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <appSettings>
  4. <add key="SourceSQLConnORC" value="############"/>
  5. <add key="SourceSQLConnVRS" value="############"/>
  6. <!-- TimerInterval unit is min -->
  7. <add key="TimerInterval" value="30"/>
  8. <add key="SourceDataPath" value="D:\ATS\Application\C#\ETest\XMLMatrixConvert\XMLMatrixConvert\bin\Debug\SourceDataPath"/>
  9. <add key="MappingFolderPath" value="D:\ATS\Application\C#\ETest\XMLMatrixConvert\XMLMatrixConvert\bin\Debug\TargetDataPath\eTest"/>
  10. </appSettings>
  11. <startup>
  12. <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  13. </startup>
  14. </configuration>

LogInfo

  1. using System;
  2. using System.IO;
  3. namespace XMLMatrixConvert.GeneralFunctions
  4. {
  5. class WriteLog
  6. {
  7. //Record log
  8. public static void WriteLogFun(String msg)
  9. {
  10. string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
  11. string tempfileFolder = Path.Combine(logPath, "Log");
  12. if (!Directory.Exists(tempfileFolder))
  13. {
  14. Directory.CreateDirectory(tempfileFolder);
  15. }
  16. string tempfile = Path.Combine(tempfileFolder, string.Format("Log_{0}.log", DateTime.Now.ToString("yyyyMMdd")));
  17. if (!File.Exists(tempfile))
  18. {
  19. File.Create(tempfile).Close();
  20. }
  21. StreamWriter writer = null;
  22. try
  23. {
  24. writer = File.AppendText(tempfile);
  25. writer.WriteLine("{0}: {1}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), msg);
  26. writer.Flush();
  27. }
  28. catch
  29. {
  30. }
  31. finally
  32. {
  33. if (writer != null)
  34. {
  35. writer.Close();
  36. }
  37. }
  38. }
  39. }
  40. }

ReadAndSetConfigFile

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. namespace XMLMatrixConvert.GeneralFunctions
  5. {
  6. class ReadAndSetConfigFile
  7. {
  8. //Error Code
  9. const string Error_ConfigLoadFail = "Load Config file is Fail.";
  10. const string Error_ConfigSetFail = "Set Config file is Fail";
  11. const string Error_SQL_Fail = "Query SQL DB fail.";
  12. /// <summary>
  13. /// 查询AppConfig中Key = Key1的value
  14. /// </summary>
  15. /// <param name="Key1">Key ID</param>
  16. /// <returns></returns>
  17. public static string GetAppConfig(string Key1)
  18. {
  19. string str = string.Empty;
  20. try
  21. {
  22. ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
  23. foreach (string key in ConfigurationManager.AppSettings)
  24. {
  25. if (key == Key1)
  26. {
  27. str = ConfigurationManager.AppSettings[Key1];
  28. }
  29. }
  30. return str;
  31. }
  32. catch
  33. {
  34. return str;
  35. }
  36. }
  37. /// <summary>
  38. /// 查询AppConfig, contain Key1的Key, 并添加value到List , 返回List<string>
  39. /// </summary>
  40. /// <param name="Key1">Key ID</param>
  41. /// <returns></returns>
  42. public static List<string> GetAppConfig_Contain(string Key1)
  43. {
  44. List<string> TempList = new List<string>();
  45. try
  46. {
  47. ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
  48. foreach (string key in ConfigurationManager.AppSettings)
  49. {
  50. if (key.ToUpper().Contains(Key1.ToUpper()))
  51. {
  52. if (ConfigurationManager.AppSettings[key] != string.Empty)
  53. {
  54. TempList.Add(ConfigurationManager.AppSettings[key]);
  55. }
  56. }
  57. }
  58. return TempList;
  59. }
  60. catch
  61. {
  62. TempList = new List<string>();
  63. TempList.Add(Error_ConfigLoadFail);
  64. return TempList;
  65. }
  66. finally
  67. {
  68. }
  69. }
  70. /// <summary>
  71. /// Search config items which name contain key1 and key2
  72. /// </summary>
  73. /// <param name="Key1"></param>
  74. /// <param name="Key2"></param>
  75. /// <returns></returns>
  76. public static List<string> GetAppConfig_Contain(string Key1, string Key2)
  77. {
  78. List<string> TempList = new List<string>();
  79. try
  80. {
  81. ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
  82. foreach (string key in ConfigurationManager.AppSettings)
  83. {
  84. if (key.ToUpper().Contains(Key1.ToUpper()) && key.ToUpper().Contains(Key2.ToUpper()))
  85. {
  86. if (ConfigurationManager.AppSettings[key] != string.Empty)
  87. {
  88. TempList.Add(ConfigurationManager.AppSettings[key]);
  89. }
  90. }
  91. }
  92. return TempList;
  93. }
  94. catch
  95. {
  96. TempList = new List<string>();
  97. TempList.Add(Error_ConfigLoadFail);
  98. return TempList;
  99. }
  100. finally
  101. {
  102. }
  103. }
  104. /// <summary>
  105. /// 修改AppConfig中Key1的value
  106. /// </summary>
  107. /// <param name="Key1">Key ID</param>
  108. /// <param name="value1">Key Value</param>
  109. public static void SetAppConfig(string Key1, string value1)
  110. {
  111. Configuration AppConfig = null;
  112. try
  113. {
  114. AppConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  115. AppSettingsSection app = AppConfig.AppSettings;
  116. if (app.Settings[Key1] == null)
  117. {
  118. app.Settings.Add(Key1, value1);
  119. }
  120. else
  121. {
  122. app.Settings[Key1].Value = value1;
  123. }
  124. AppConfig.Save(ConfigurationSaveMode.Modified);
  125. ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件
  126. }
  127. catch (Exception Ex)
  128. {
  129. throw new Exception(Error_ConfigSetFail + Ex.Message);
  130. }
  131. finally
  132. {
  133. AppConfig = null;
  134. }
  135. }
  136. }
  137. }

PaserParameter

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace XMLMatrixConvert.GeneralFunctions
  7. {
  8. class PaserParameter
  9. {
  10. public static string PaserString(string msg, string key, string spliter)
  11. {
  12. string result = string.Empty;
  13. if (!msg.Contains(key))
  14. {
  15. return result;
  16. }
  17. int indexKey = msg.IndexOf(key, 0);
  18. int indexSpliter = msg.IndexOf(spliter, indexKey);
  19. if (indexSpliter == -1)
  20. {
  21. result = msg.Substring(indexKey).Replace(key, "");
  22. }
  23. else
  24. {
  25. result = msg.Substring(indexKey, indexSpliter - indexKey).Replace(key, "");
  26. }
  27. return result;
  28. }
  29. public static string GetParentFolderFullPath(string filePath)
  30. {
  31. string folderPath = string.Empty;
  32. int lastChar = filePath.LastIndexOf(@"\");
  33. folderPath = filePath.Substring(0, lastChar);
  34. return folderPath;
  35. }
  36. }
  37. }

Properies

图片.png