153 lines
4.5 KiB
C#
153 lines
4.5 KiB
C#
// See https://aka.ms/new-console-template for more information
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Data.SqlTypes;
|
|
using System.Diagnostics;
|
|
|
|
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())
|
|
{
|
|
file.Delete();
|
|
}
|
|
|
|
foreach (DirectoryInfo subDirectory in directory.GetDirectories())
|
|
{
|
|
subDirectory.Delete(true);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
var configFolder = $@"C:\ProgramData\.xingcang\";
|
|
|
|
var configPath =Path.Combine(configFolder, "config.json") ;
|
|
if (!File.Exists(configPath))
|
|
{
|
|
Console.WriteLine("当前系统未部署影像系统,无须卸载");
|
|
Console.ReadLine();
|
|
}
|
|
else
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
Console.WriteLine("");
|
|
Console.WriteLine("警告:卸载部署的网站,会将数据库,部署的网站,以及运行的所有数据记录都会清除,该操作非常慎重,请确保你已知晓该后果");
|
|
Console.WriteLine(" 您确定要卸载该程序吗?(Y/N)");
|
|
Console.ResetColor();
|
|
|
|
string confirm = Console.ReadLine();
|
|
if (confirm.ToUpper() == "Y")
|
|
{
|
|
// 执行卸载操作
|
|
try
|
|
{
|
|
Console.WriteLine("开始读取激活信息...");
|
|
|
|
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(),
|
|
nginxPath = jObject["nginxPath"].ToString(),
|
|
apiPath = jObject["apiPath"].ToString(),
|
|
serviceName = jObject["serviceName"].ToString(),
|
|
};
|
|
Console.WriteLine("激活信息读取完毕,开始准备卸载...");
|
|
|
|
|
|
|
|
Process process = new Process();
|
|
process.StartInfo.FileName = "cmd.exe";
|
|
process.StartInfo.RedirectStandardInput = true;
|
|
process.StartInfo.UseShellExecute = false;
|
|
//process.StartInfo.CreateNoWindow = false; // 不创建新窗口
|
|
|
|
process.StartInfo.RedirectStandardOutput = true;
|
|
process.Start();
|
|
|
|
Console.WriteLine("停止nginx进程...");
|
|
|
|
var proList = Process.GetProcesses().Where(t => t.ProcessName.Contains("nginx") && t.MainModule.FileName.Contains(configObj.nginxPath));
|
|
|
|
foreach (var item in proList)
|
|
{
|
|
item.Kill();
|
|
}
|
|
|
|
|
|
|
|
Console.WriteLine("停止后端服务...");
|
|
|
|
await ProcessStandardInputAsync(process, $"sc stop {configObj.serviceName}");
|
|
await ProcessStandardInputAsync(process, $"sc delete {configObj.serviceName}");
|
|
|
|
|
|
Console.WriteLine("数据库若被其他进程或者连接正在使用,将会删除失败,您可以卸载完后手动删除遗留的数据库");
|
|
Console.WriteLine("执行删除数据库命令...");
|
|
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("删除部署文件夹下的所有内容...");
|
|
DeleteFolderContents(configObj.deployFolder);
|
|
|
|
|
|
//DeleteFolderContents(configFolder);
|
|
|
|
Console.WriteLine("删除激活信息...");
|
|
|
|
Console.WriteLine("卸载结束");
|
|
Console.ReadLine();
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
Console.WriteLine("程序出错,卸载失败:" + ex.Message);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|