1. linux环境
linux环境通过执行bash命令直接执行:
public string RunCmdLinux(string cmd)
{
var proc = new Process();
System.Console.Write($"Run Linux cmd ==> [{cmd}] START!");
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = "/bin/bash";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
proc.StandardInput.WriteLine(cmd);
proc.StandardInput.WriteLine("exit");
string outStr = proc.StandardOutput.ReadToEnd();
proc.Close();
System.Console.Write($"Run Linux cmd ==> [{cmd}] FINISHED!");
return outStr;
}
2. window环境
windows使用 System.ServiceProcess
依赖下的ServiceController
进行操作,具体代码:
/// <summary>
/// 开启windows服务
/// </summary>
/// <param name="serviceName"></param>
/// <returns></returns>
public bool StartWindowsService(string serviceName)
{
bool isbn = false;
try
{
ServiceController star_service = new ServiceController(serviceName);
if (star_service.Status != ServiceControllerStatus.Running &&
star_service.Status != ServiceControllerStatus.StartPending)
{
star_service.Start();
for (int i = 0; i < 60; i++)
{
star_service.Refresh();
System.Threading.Thread.Sleep(1000);
if (star_service.Status == ServiceControllerStatus.Running)
{
isbn = true;
break;
}
if (i == 59)
{
isbn = false;
}
}
}
}
catch (Exception ex)
{
System.Console.Write("[StartWindowsService] Error", ex);
return false;
}
System.Console.Write($"[StartWindowsService]_serviceName:{serviceName},status:{isbn}");
return isbn;
}
/// <summary>
/// 关闭windows服务
/// </summary>
/// <param name="serviceName"></param>
/// <returns></returns>
public bool StopWindowsService(string serviceName)
{
bool isbn = false;
try
{
ServiceController star_service = new ServiceController(serviceName);
System.Console.WriteLine(star_service.Status);
if (star_service.Status == ServiceControllerStatus.Running)
{
star_service.Stop();
}
for (int i = 0; i < 60; i++)
{
star_service.Refresh();
System.Threading.Thread.Sleep(1000);
if (star_service.Status == ServiceControllerStatus.Running)
{
isbn = true;
break;
}
if (i == 59)
{
isbn = false;
}
}
}
catch (Exception ex)
{
System.Console.Write("[StopWindowsService] Error", ex);
isbn = false;
}
System.Console.Write($"[StopWindowsService]_serviceName:{serviceName},status:{isbn}");
return isbn;
}
/// <summary>
/// 获取windows服务的状态
/// </summary>
/// <param name="serviceName">服务名</param>
/// <returns>服务的状态</returns>
public string CheckWindowsServiceStatus(string serviceName) {
var services = ServiceController.GetServices();
string serviceStatu = string.Empty;
bool isServiceExist = false;
foreach (var s in services)
{
if (s.ServiceName.ToLower().Equals(serviceName.ToLower()))
{
System.Console.Write($"[CheckWindowsServiceStatus] serviceName:{serviceName},serviceStatus:{s.Status}");
isServiceExist = true;
serviceStatu = s.Status.ToString();
break;
}
}
if (!isServiceExist)
{
System.Console.Write($"[CheckWindowsServiceStatus] serviceName:${serviceName} not instal");
serviceStatu = "Service is not installed";
}
return serviceStatu;
}
当服务不能停止时,还可根据服务名,通过进程进行杀死:
/// <summary>
/// 通过服务的名称查询进程号,进行杀死
/// </summary>
/// <param name="threadName">服务名称</param>
/// <returns></returns>
public void ThreadKillExitis(string threadName)
{
Process[] processList = Process.GetProcesses();
foreach (Process process in processList)
{
if (process.ProcessName.ToLower() == threadName.ToLower())
{
process.Kill();
}
}
System.Console.Write($"[ThreadKillExitis]_{threadName} 成功");
}
权限配置:
由于windows环境运行需要获取系统权限,因此需要设置app.manifest内容。
首先右键项目->属性->安全性下勾选启用ClickOnce安全设置,会默认增加一个app.manifest文件。
打开后,将<requestedExecutionLevel level="asInvoker" uiAccess="false" />
修改为<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
完成后,右键项目->属性->安全性下取消勾选启用ClickOnce安全设置