This commit is contained in:
2023-04-24 14:08:17 +08:00
parent 80676f1496
commit ff6ef1f5d6
14 changed files with 912 additions and 230 deletions
+71 -19
View File
@@ -1,8 +1,10 @@
// 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;
async Task ProcessStandardInputAsync(Process process, string cmd, string workDirectory = "")
{
@@ -30,7 +32,36 @@ void DeleteFolderContents(string path)
foreach (FileInfo file in directory.GetFiles())
{
file.Delete();
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())
@@ -39,23 +70,40 @@ void DeleteFolderContents(string path)
}
}
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("Start")).ToList();
foreach (var item in startProList)
{
item.Kill();
}
var configFolder = $@"C:\ProgramData\.xingcang\";
var configPath =Path.Combine(configFolder, "config.json") ;
if (!File.Exists(configPath))
{
Console.WriteLine("当前系统未部署影像系统,无须卸载");
MyLog("当前系统未部署影像系统,无须卸载");
Console.ReadLine();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("");
Console.WriteLine("警告:卸载EI Image Viewer 服务,会将后台服务、数据库、网站文件以及运行的所有数据记录清除,请慎重操作。在卸载前,请确保数据已经备份。");
Console.WriteLine(" 您确定要继续卸载该服务吗?(Y/N)");
MyLog("");
MyLog("警告:卸载EI Image Viewer 服务,会将后台服务、数据库、网站文件以及运行的所有数据记录清除,请慎重操作。在卸载前,请确保数据已经备份。");
MyLog(" 您确定要继续卸载该服务吗?(Y/N)");
Console.ResetColor();
string confirm = Console.ReadLine();
@@ -64,7 +112,7 @@ else
// 执行卸载操作
try
{
Console.WriteLine("开始读取激活信息...");
MyLog("开始读取激活信息...");
var appsettingsJson = File.ReadAllText(configPath);
@@ -87,7 +135,7 @@ else
nginxServiceName= jObject["nginxServiceName"]?.ToString(),
nginxServiceEXEPath= jObject["nginxServiceEXEPath"]?.ToString(),
};
Console.WriteLine("激活信息读取完毕,开始准备卸载...");
MyLog("激活信息读取完毕,开始准备卸载...");
@@ -101,14 +149,15 @@ else
process.Start();
Console.WriteLine("停止并卸载nginx服务...");
MyLog("停止并卸载nginx服务...");
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));
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)
{
@@ -117,37 +166,40 @@ else
Console.WriteLine("停止并卸载后端服务...");
MyLog("停止并卸载后端服务...");
await ProcessStandardInputAsync(process, $"sc stop {configObj.serviceName}");
await ProcessStandardInputAsync(process, $"sc delete {configObj.serviceName}");
Console.WriteLine("数据库若被其他进程或者连接正在使用,将会删除失败,您可以卸载完后手动删除遗留的数据库");
Console.WriteLine("执行删除数据库命令...");
MyLog("数据库若被其他进程或者连接正在使用,将会删除失败,您可以卸载完后手动删除遗留的数据库");
MyLog("执行删除数据库命令...");
await ProcessStandardInputAsync(process, $@" SQLCMD -S {configObj.server} -U {configObj.user} -P {configObj.password} -Q ""DROP DATABASE {configObj.dbName}"" ");
process.StandardInput.Close();
process.WaitForExit();
Console.WriteLine("删除部署文件夹下的所有内容...");
MyLog("删除部署文件夹下的所有内容...");
DeleteFolderContents(configObj.deployFolder);
DeleteFolderContents(configFolder);
Console.WriteLine("删除激活信息...");
Console.WriteLine("EI Image Viewer 服务卸载完成。");
Console.ReadLine();
MyLog("删除激活信息...");
MyLog("EI Image Viewer 服务卸载完成。");
}
catch (Exception ex)
{
Console.WriteLine("程序出错:" + ex.Message);
MyLog("程序出错:" + ex.Message);
}
finally
{
Console.ReadLine();
}
}
+1
View File
@@ -15,6 +15,7 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>
</Project>