123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Threading;
- namespace Vinno.FIS.Sonopost.Upgrade
- {
- public class UpgradeOperator
- {
- private string _upgradeFilePath;
- private string _targetPath;
- private bool _deleteAllFiles;
- private string _sonopostFolderPath;
- public UpgradeOperator(string upgradePath, string targetPath, bool deleteAllFiles)
- {
- _upgradeFilePath = upgradePath;
- _targetPath = targetPath;
- _deleteAllFiles = deleteAllFiles;
- var rootPath = Path.GetPathRoot(targetPath);
- _sonopostFolderPath = Path.Combine(rootPath, "Sonopost");
- }
- public bool StartUpgrade()
- {
- if (!Directory.Exists(_upgradeFilePath))
- {
- return false;
- }
- UninstallSonopostTask();
- StopSonopostKeeper();
- StopSonopost();
- StopTRTCClient();
- RunUpgrade();
- InstallSonopostTask();
- return true;
- }
- private void DeleteAllFiles()
- {
- if (!_deleteAllFiles)
- {
- return;
- }
- DeleteDirectory(_sonopostFolderPath);
- }
- private void RestartSonopostService()
- {
- ServiceHelper.Install();
- ServiceHelper.Start();
- }
- public void InstallSonopostTask()
- {
- TaskSchedulerHelper.Install();
- }
- private void RunUpgrade()
- {
- MoveFiles(_upgradeFilePath, _targetPath);
- DeleteEmptyFolder(_targetPath);
- DeleteDirectory(_upgradeFilePath);
- DeleteAllFiles();
- }
- private void StopSonopostService()
- {
- ServiceHelper.Stop();
- ServiceHelper.UnInstall();
- }
- private void UninstallSonopostTask()
- {
- TaskSchedulerHelper.UnInstall();
- }
- private void StopSonopost()
- {
- FinishProcessByName("Sonopost");
- }
- private void StopTRTCClient()
- {
- FinishProcessByName("Vinno.FIS.TRTCClient");
- }
- private void StopSonopostKeeper()
- {
- FinishProcessByName("Vinno.FIS.Sonopost.Keeper");
- }
- private void MoveFiles(string srcFolder, string targetFolder)
- {
- if (!Directory.Exists(targetFolder))
- {
- Directory.CreateDirectory(targetFolder);
- }
- var files = Directory.GetFiles(srcFolder);
- foreach (var file in files)
- {
- try
- {
- File.Copy(file, file.Replace(srcFolder, targetFolder), true);
- }
- catch (Exception ex)
- {
- File.AppendAllText("UpgradeLog.txt", ex.ToString());
- }
- }
- var dirs = Directory.GetDirectories(srcFolder);
- foreach (var dir in dirs)
- {
- if (!dir.EndsWith("SonopostUpgrade"))
- {
- MoveFiles(dir, dir.Replace(srcFolder, targetFolder));
- }
- }
- }
- /// <summary>
- /// Delete directory
- /// </summary>
- /// <param name="directory"></param>
- private static bool DeleteDirectory(string directory)
- {
- try
- {
- if (!Directory.Exists(directory))
- {
- return true;
- }
- var files = Directory.GetFiles(directory);
- var directories = Directory.GetDirectories(directory);
- foreach (var file in files)
- {
- File.SetAttributes(file, FileAttributes.Normal);
- File.Delete(file);
- }
- foreach (var dir in directories)
- {
- DeleteDirectory(dir);
- }
- Directory.Delete(directory, false);
- }
- catch (Exception ex)
- {
- File.AppendAllText("UpgradeLog.txt", ex.ToString());
- return false;
- }
- return true;
- }
- private bool DeleteEmptyFolder(string folder)
- {
- bool allChildFolderIsDelete = true;
- var files = Directory.GetFiles(folder);
- var childFolders = Directory.GetDirectories(folder);
- if (files.Length > 0)
- {
- if (childFolders.Length > 0)
- {
- foreach (var childFolder in childFolders)
- {
- if (DeleteEmptyFolder(childFolder) == false)
- {
- allChildFolderIsDelete = false;
- }
- }
- }
- return false;
- }
- else if (childFolders.Length > 0)
- {
- foreach (var childFolder in childFolders)
- {
- if (DeleteEmptyFolder(childFolder) == false)
- {
- allChildFolderIsDelete = false;
- }
- }
- }
- else
- {
- DeleteDirectory(folder);
- return true;
- }
- if (allChildFolderIsDelete && Directory.Exists(folder))
- {
- DeleteDirectory(folder);
- return true;
- }
- return false;
- }
- private bool FinishProcessByName(string name)
- {
- int count = 5;
- while (count > 0)
- {
- var existApps = Process.GetProcessesByName(name).Where(x => x.ProcessName == name);
- if (!existApps.Any())
- {
- return true;
- }
- foreach (var item in existApps)
- {
- item.Kill();
- Thread.Sleep(1000);
- }
- count--;
- }
- return false;
- }
- }
- }
|