225 lines
6.9 KiB
C#
225 lines
6.9 KiB
C#
// See https://aka.ms/new-console-template for more information
|
||
|
||
using Newtonsoft.Json.Linq;
|
||
using Serilog;
|
||
using System.Data.SqlTypes;
|
||
using System.Diagnostics;
|
||
using System.Drawing;
|
||
using static System.Net.Mime.MediaTypeNames;
|
||
|
||
async Task ProcessStandardInputAsync(Process process, string cmd, string workDirectory = "")
|
||
{
|
||
if (!string.IsNullOrWhiteSpace(workDirectory))
|
||
{
|
||
process.StartInfo.WorkingDirectory = workDirectory;
|
||
}
|
||
|
||
await process.StandardInput.WriteLineAsync(cmd);
|
||
await process.StandardInput.FlushAsync();
|
||
|
||
while (!process.StandardOutput.EndOfStream)
|
||
{
|
||
var output = await process.StandardOutput.ReadLineAsync();
|
||
if (string.IsNullOrWhiteSpace(output))
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
void DeleteFolderContents(string path)
|
||
{
|
||
DirectoryInfo directory = new DirectoryInfo(path);
|
||
|
||
foreach (FileInfo file in directory.GetFiles())
|
||
{
|
||
if (file.FullName.Contains("UnInstall"))
|
||
{
|
||
continue;
|
||
}
|
||
|
||
try
|
||
{
|
||
file.Attributes = FileAttributes.Normal;
|
||
file.Delete();
|
||
}
|
||
catch (IOException ex)
|
||
{
|
||
// 处理文件被占用的异常
|
||
MyLog($"File {file.FullName} is being used and cannot be deleted. {ex.Message}");
|
||
}
|
||
catch (UnauthorizedAccessException ex)
|
||
{
|
||
|
||
try
|
||
{
|
||
File.Delete(file.FullName);
|
||
}
|
||
catch (Exception ex2)
|
||
{
|
||
|
||
MyLog($"File {file.FullName} cannot be deleted. {ex2.Message}");
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
foreach (DirectoryInfo subDirectory in directory.GetDirectories())
|
||
{
|
||
subDirectory.Delete(true);
|
||
}
|
||
}
|
||
|
||
|
||
void MyLog(string message)
|
||
{
|
||
Console.WriteLine(message);
|
||
//Serilog.Log.Information(message);
|
||
}
|
||
|
||
|
||
//Serilog.Log.Logger = new LoggerConfiguration()
|
||
// .WriteTo.File("logs\\UninstallLog.txt", rollingInterval: RollingInterval.Day)
|
||
// .CreateLogger();
|
||
|
||
|
||
|
||
var startProList = Process.GetProcesses().Where(t => t.ProcessName.Contains("EI_Image_Viewer_Installer")).ToList();
|
||
foreach (var item in startProList)
|
||
{
|
||
item.Kill();
|
||
}
|
||
|
||
|
||
var configFolder = $@"C:\ProgramData\.xingcang\";
|
||
|
||
var configPath = Path.Combine(configFolder, "config.json");
|
||
if (!File.Exists(configPath))
|
||
{
|
||
MyLog("LiLi DICOM Viewer has not been installed. No need to uninstall.");
|
||
Console.ReadLine();
|
||
}
|
||
else
|
||
{
|
||
Console.ForegroundColor = ConsoleColor.Red;
|
||
MyLog("");
|
||
MyLog("Warning: uninstalling LiLi DICOM Viewer service will clear backend service, database, website files and all data records of the operation. Please make sure data has been backed up.");
|
||
MyLog(" Continue to uninstall the service?(Y/N)");
|
||
Console.ResetColor();
|
||
|
||
string confirm = Console.ReadLine();
|
||
if (confirm.ToUpper() == "Y")
|
||
{
|
||
// 执行卸载操作
|
||
try
|
||
{
|
||
MyLog("Start reading the activation information...");
|
||
|
||
var appsettingsJson = File.ReadAllText(configPath);
|
||
|
||
// 解析 JSON 字符串
|
||
var jObject = JObject.Parse(appsettingsJson);
|
||
|
||
|
||
var configObj = new
|
||
{
|
||
key = jObject["key"].ToString(),
|
||
value = jObject["value"].ToString(),
|
||
user = jObject["user"].ToString(),
|
||
server = jObject["server"].ToString(),
|
||
password = jObject["password"].ToString(),
|
||
dbName = jObject["dbName"].ToString(),
|
||
deployFolder = jObject["deployFolder"].ToString(),
|
||
nginxStartPath = jObject["nginxStartPath"].ToString(),
|
||
//apiPath = jObject["apiPath"].ToString(),
|
||
serviceName = jObject["serviceName"].ToString(),
|
||
nginxServiceName = jObject["nginxServiceName"]?.ToString(),
|
||
nginxServiceEXEPath = jObject["nginxServiceEXEPath"]?.ToString(),
|
||
};
|
||
MyLog("Activation information has been read. Prepare to uninstall…");
|
||
|
||
Process process = new Process();
|
||
process.StartInfo.FileName = "cmd.exe";
|
||
process.StartInfo.RedirectStandardInput = true;
|
||
process.StartInfo.UseShellExecute = false;
|
||
process.StartInfo.RedirectStandardOutput = true;
|
||
|
||
//process.StartInfo.CreateNoWindow = false; // 不创建新窗口
|
||
|
||
process.Start();
|
||
|
||
MyLog("Stop and uninstall Nginx service...");
|
||
await ProcessStandardInputAsync(process, $" {configObj.nginxServiceEXEPath} uninstall ", configObj.nginxStartPath);
|
||
await ProcessStandardInputAsync(process, $"sc stop {configObj.nginxServiceName}");
|
||
await ProcessStandardInputAsync(process, $"sc delete {configObj.nginxServiceName}");
|
||
|
||
await Task.Delay(2000);
|
||
|
||
|
||
var proList = Process.GetProcesses().Where(t => ((t.ProcessName.Contains("nginx") || t.ProcessName.Contains("nginxService")) && t.MainModule.FileName.Contains(configObj.nginxStartPath))).ToList();
|
||
|
||
foreach (var item in proList)
|
||
{
|
||
item.Kill();
|
||
}
|
||
|
||
|
||
|
||
|
||
MyLog("Stop and uninstall the backend service.");
|
||
|
||
await ProcessStandardInputAsync(process, $"sc stop {configObj.serviceName}");
|
||
await ProcessStandardInputAsync(process, $"sc delete {configObj.serviceName}");
|
||
|
||
|
||
MyLog("The database cannot be deleted while being used by other processes or connections. After uninstallation, please delete the remaining database manually.");
|
||
MyLog("Execute the command of deleting database.");
|
||
await ProcessStandardInputAsync(process, $@" SQLCMD -S {configObj.server} -U {configObj.user} -P {configObj.password} -Q ""DROP DATABASE {configObj.dbName}"" ");
|
||
|
||
|
||
process.StandardInput.Close();
|
||
process.WaitForExit();
|
||
|
||
await Task.Delay(3000);
|
||
|
||
MyLog($"Delete all deletable contents under the deployment folder {configObj.deployFolder}...");
|
||
DeleteFolderContents(configObj.deployFolder);
|
||
|
||
|
||
MyLog("Delete activation information...");
|
||
DeleteFolderContents(configFolder);
|
||
MyLog("The LiLi DICOM Viewer service has been uninstalled.");
|
||
MyLog("Will automatically exit in 3 seconds…");
|
||
await Task.Delay(3000);
|
||
|
||
string cmdArgs = $"/C ping 127.0.0.1 -n 3 & rmdir /s /q \"{configObj.deployFolder}\"";
|
||
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", cmdArgs);
|
||
psi.WindowStyle = ProcessWindowStyle.Hidden;
|
||
psi.CreateNoWindow = true;
|
||
Process.Start(psi);
|
||
|
||
Environment.Exit(0);
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
|
||
MyLog("Problems encountered during the uninstallation process: " + ex.Message + " Please check whether other applications are occupying relevant resources. Once confirmed, try to uninstall again.");
|
||
Console.ReadLine();
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
|