120 lines
3.6 KiB
C#
120 lines
3.6 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Text;
|
|||
|
using System.Runtime.InteropServices;
|
|||
|
using System.ServiceProcess;
|
|||
|
using System.Diagnostics;
|
|||
|
|
|||
|
namespace Server.Utils
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 安装Windows服务业务层
|
|||
|
/// </summary>
|
|||
|
public class ServiceInstaller
|
|||
|
{
|
|||
|
|
|||
|
public static ServiceController GetServiceController(string serviceName)
|
|||
|
{
|
|||
|
ServiceController[] controllers = ServiceController.GetServices();
|
|||
|
int nNum = controllers.Length;
|
|||
|
try
|
|||
|
{
|
|||
|
for (int i = 0; i <= (nNum - 1); i++)
|
|||
|
{
|
|||
|
if ((controllers[i].ServiceName.ToUpper() == serviceName.ToUpper()))
|
|||
|
{
|
|||
|
var v = controllers[i];
|
|||
|
return v;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 执行DOS命令,返回DOS命令的输出
|
|||
|
/// </summary>
|
|||
|
/// <param name="dosCommand">dos命令</param>
|
|||
|
/// <param name="milliseconds">等待命令执行的时间(单位:毫秒),
|
|||
|
/// 如果设定为0,则无限等待</param>
|
|||
|
/// <returns>返回DOS命令的输出</returns>
|
|||
|
public static string Execute(string command, int seconds)
|
|||
|
{
|
|||
|
string output = ""; //输出字符串
|
|||
|
if (command != null && !command.Equals(""))
|
|||
|
{
|
|||
|
var pro = new Process();
|
|||
|
pro.StartInfo.FileName = "cmd.exe";
|
|||
|
pro.StartInfo.UseShellExecute = false;
|
|||
|
pro.StartInfo.CreateNoWindow = true;
|
|||
|
pro.StartInfo.RedirectStandardInput = true;
|
|||
|
pro.StartInfo.RedirectStandardOutput = true;
|
|||
|
pro.StartInfo.RedirectStandardError = true;
|
|||
|
pro.OutputDataReceived += (sender, e) => Console.WriteLine(e.Data);
|
|||
|
pro.ErrorDataReceived += (sender, e) => Console.WriteLine(e.Data);
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
if (pro.Start())//开始进程
|
|||
|
{
|
|||
|
var sIn = pro.StandardInput;
|
|||
|
sIn.AutoFlush = true;
|
|||
|
var Codes = command.Split(new string[] {"\r\n"}, StringSplitOptions.None);
|
|||
|
|
|||
|
pro.BeginOutputReadLine();
|
|||
|
|
|||
|
foreach (var code in Codes)
|
|||
|
{
|
|||
|
sIn.WriteLine(code);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
if (seconds == 0)
|
|||
|
{
|
|||
|
pro.WaitForExit();//这里无限等待进程结束
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
pro.WaitForExit(seconds); //等待进程结束,等待时间为指定的毫秒
|
|||
|
}
|
|||
|
output = pro.StandardOutput.ReadToEnd();//读取进程的输出
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Console.WriteLine(ex.Message);//捕获异常,输出异常信息
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
if (pro != null)
|
|||
|
pro.Close();
|
|||
|
}
|
|||
|
}
|
|||
|
return output;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
public static string InitServer(string Path,string Name)
|
|||
|
{
|
|||
|
var code = $@"@echo off
|
|||
|
net stop {Name}
|
|||
|
{Path} --remove {Name}
|
|||
|
{Path} --install {Name}
|
|||
|
net start {Name}";
|
|||
|
return Execute(code,20);
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|