Patch.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Data;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using System.Data.SqlClient;
  6. using System.IO;
  7. using System.Diagnostics;
  8. using System.IO.Compression;
  9. using System.ComponentModel;
  10. using System.Collections.Generic;
  11. namespace Patch
  12. {
  13. public partial class Patch : Form
  14. {
  15. private const string TARGET_PROGRAM = "DataManager.exe";
  16. private List<string> _IgnoreFileList = new List<string>() {
  17. "DB_CONFIG.ini",
  18. "Log.xml"
  19. };
  20. public Patch()
  21. {
  22. InitializeComponent();
  23. BackgroundWorker backgroundWorker = new BackgroundWorker();
  24. backgroundWorker.DoWork += new DoWorkEventHandler(ExecutePatch);
  25. backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ExecuteTargetProgram);
  26. backgroundWorker.RunWorkerAsync();
  27. }
  28. private void ExecutePatch(object sender, DoWorkEventArgs e)
  29. {
  30. string downloadPath = Path.GetDirectoryName(Application.StartupPath);
  31. string targetPath = Path.Combine(downloadPath, $"___{DateTime.Now.ToString("yyyyMMddHHmmssfff")}");
  32. if (!Directory.Exists(targetPath))
  33. Directory.CreateDirectory(targetPath);
  34. string downloadFileName = string.Empty;
  35. try
  36. {
  37. // 데이터베이스에서 파일 목록 가져오기
  38. DataTable fileData = GetFileDataFromDatabase(GetFileVersion());
  39. if (fileData.Rows.Count > 0)
  40. {
  41. DataRow dataRow = fileData.Rows[0];
  42. string fileName = dataRow["FileName"].ToString();
  43. byte[] fileContent = (byte[])dataRow["FileContent"];
  44. downloadFileName = CreateFile(fileName, fileContent, downloadPath);
  45. ZipFile.ExtractToDirectory(downloadFileName, targetPath);
  46. // 임시 폴더 파일을 원래 폴더에 덮어쓰기
  47. string[] files = Directory.GetFiles(targetPath, "*.*", SearchOption.AllDirectories);
  48. // 각 파일을 목적지 디렉토리로 복사합니다.
  49. foreach (string file in files)
  50. {
  51. if (_IgnoreFileList.Contains(Path.GetFileName(file)))
  52. continue;
  53. // 파일 이름만 추출하여 목적지 경로에 추가합니다.
  54. string destinationFile = file.Replace(targetPath, Application.StartupPath);
  55. string destinationDirectory = Path.GetDirectoryName(destinationFile);
  56. if (!Directory.Exists(destinationDirectory))
  57. Directory.CreateDirectory(destinationDirectory);
  58. // 파일을 덮어쓰기 위해 복사합니다.
  59. File.Copy(file, destinationFile, true);
  60. }
  61. }
  62. }
  63. catch (Exception ex) { }
  64. finally
  65. {
  66. if (Directory.Exists(targetPath))
  67. Directory.Delete(targetPath, true);
  68. if (File.Exists(downloadFileName))
  69. File.Delete(downloadFileName);
  70. }
  71. }
  72. private void ExecuteTargetProgram(object sender, RunWorkerCompletedEventArgs e)
  73. {
  74. string targetPath = Path.Combine(Application.StartupPath, TARGET_PROGRAM);
  75. if (File.Exists(targetPath))
  76. {
  77. ProcessStartInfo processStartInfo = new ProcessStartInfo
  78. {
  79. FileName = targetPath,
  80. UseShellExecute = true,
  81. Verb = "runas" // 관리자 권한으로 실행
  82. };
  83. Process.Start(processStartInfo);
  84. }
  85. Close();
  86. }
  87. private string GetFileVersion()
  88. {
  89. string targetPath = Path.Combine(Application.StartupPath, TARGET_PROGRAM);
  90. if (File.Exists(targetPath))
  91. return FileVersionInfo.GetVersionInfo(targetPath).FileVersion;
  92. else
  93. return string.Empty;
  94. }
  95. private DataTable GetFileDataFromDatabase(string fileVersion)
  96. {
  97. string connectionString = $"Server=sf.tascorp.co.kr,1444;database=_GIT_MIRROR_DB;uid=sa;pwd=!1Tascorp";
  98. using (SqlConnection conn = new SqlConnection(connectionString))
  99. {
  100. string query = @"
  101. SELECT FileName, FileContent
  102. FROM Patch
  103. WHERE IsCurrentYN = 'Y'
  104. AND FileVersion <> '@FILE_VERSION'
  105. ";
  106. query = query.Replace("@FILE_VERSION", fileVersion);
  107. SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
  108. DataTable fileData = new DataTable();
  109. adapter.Fill(fileData);
  110. return fileData;
  111. }
  112. }
  113. private string CreateFile(string fileName, byte[] fileContent, string targetPath)
  114. {
  115. // 파일이 존재하는지 확인하고, 존재하지 않으면 파일 생성
  116. string filePath = Path.Combine(targetPath, fileName);
  117. if (!File.Exists(filePath))
  118. File.WriteAllBytes(filePath, fileContent);
  119. else
  120. {
  121. // 파일 내용 비교 후 다를 경우 파일 생성
  122. byte[] existingFileContent = File.ReadAllBytes(filePath);
  123. if (!fileContent.SequenceEqual(existingFileContent))
  124. File.WriteAllBytes(filePath, fileContent);
  125. }
  126. return filePath;
  127. }
  128. }
  129. }