EI-Image-Viewer-Api/Install/Program.cs

61 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// See https://aka.ms/new-console-template for more information
using System.Diagnostics;
using System.ServiceProcess;
string nginxPath = @"D:\test\EIImageViewerWeb\nginx.exe";
string command = $"create EI_Nginx binPath= \"{nginxPath} -p D:\\test\\EIImageViewerWeb\\\" start= auto DisplayName= \"EI_Nginx_DisplayName\"";
try
{
// 使用SC命令创建Nginx服务
using (var process = new System.Diagnostics.Process())
{
process.StartInfo.FileName = "sc";
process.StartInfo.Arguments = command;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.WaitForExit();
// 检查服务是否已成功创建
if (process.ExitCode != 0)
{
throw new Exception($"Error creating service: {process.StandardOutput.ReadToEnd()}");
}
}
// 启动Nginx服务
using (var controller = new ServiceController("EI_Nginx"))
{
controller.Start();
}
Console.WriteLine("Nginx service created and started successfully.");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
string currentDir = Directory.GetCurrentDirectory();
DirectoryInfo dirInfo = new DirectoryInfo(currentDir);
foreach (var subdir in dirInfo.GetDirectories())
{
FileInfo[] files = subdir.GetFiles();
foreach (FileInfo file in files)
{
if (file.Name == "Start.exe")
{
string startPath = Path.Combine(subdir.FullName, "Start.exe");
Process.Start(startPath);
return;
}
}
}
Console.WriteLine("当前目录子文件中未找到Start.exe程序即将退出...");
Console.ReadLine();