1. 程式人生 > 程式設計 >c# 將Minio.exe註冊成windows服務

c# 將Minio.exe註冊成windows服務

minio 註冊成windows 服務的工具開發

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
  public partial class Main : Form
  {
    public Main()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender,EventArgs e)
    {
      // 註冊服務
      var script= this.CreateXmlContent();

      try
      {
        using (Runspace runspace = RunspaceFactory.CreateRunspace())
        {
          runspace.Open();
          PowerShell ps = PowerShell.Create();
          ps.Runspace = runspace;
          ps.AddScript(script);
          ps.Invoke();
        }

        Thread.Sleep(2000);
        // 啟動服務
        StartService();

        MessageBox.Show(@"服務啟動成功");
      }
      catch (Exception ex)
      {
        MessageBox.Show(@"註冊失敗");
      }
    }

    private string CreateXmlContent()
    {
      var filePath = Path.Combine(Directory.GetCurrentDirectory(),"minio-service.ps1");
      if (!File.Exists(filePath))
      {
        File.Create(filePath).Close();
      }

      var content =
        "if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] \"Administrator\")) { Start-Process powershell.exe \"-NoProfile -ExecutionPolicy Bypass -File `\"$PSCommandPath`\"\" -Verb RunAs; exit }";
      content += "Set-Location -Path $PSScriptRoot\r\n\r\n";
      content += "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12\r\n";
      content += "$config = @'\r\n";
      content += "<service>\r\n";
      content += $" <id>{textBox1.Text}</id>\r\n";
      content += $" <name>{textBox1.Text}</name>\r\n";
      content += " <description>MinIO server is a nb oss server</description>\r\n";
      content += " <executable>minio.exe</executable>\r\n";
      content += $@" <env name=""MINIO_ACCESS_KEY"" value=""{textBox5.Text}"" />" + "\r\n";
      content += $@" <env name=""MINIO_SECRET_KEY"" value =""{textBox4.Text}"" />" + "\r\n";
      content += $@" <arguments>server --address 0.0.0.0:{textBox2.Text} {textBox3.Text}</arguments>" + "\r\n";
      content += @" <logmode>rotate</logmode>" + "\r\n";
      content += @" </service>" + "\r\n";
      content += @"'@" + "\r\n\r\n";
      content += @"Set-Content ""minio-service.xml"" $config" + "\r\n";
      content += @"Start-Process -WorkingDirectory $PSScriptRoot -FilePath ""$($PSScriptRoot)\minio-service.exe"" -ArgumentList ""install"" -NoNewWindow -PassThru -Wait" + "\r\n";
      content += @"Write-Host ""Installation done""";

      File.WriteAllText(filePath,content,Encoding.Default);

      return filePath;
    }

    private void Main_Load(object sender,EventArgs e)
    {
      textBox3.Text = Path.Combine(Directory.GetCurrentDirectory(),"minio");
      // 獲取資源
      var minio_service = MinioTool.Properties.Resources.minio_service;
      var exePath = Path.Combine(Directory.GetCurrentDirectory(),"minio-service.exe");
      if (!File.Exists(exePath))
      {
        File.Create(exePath).Close();
      }
      File.WriteAllBytes(exePath,minio_service);
    }

    /// <summary>
    /// 啟動服務
    /// </summary>
    private void StartService()
    {
      ServiceController[] services = ServiceController.GetServices();
      foreach (ServiceController service in services)
      {
        if (service.ServiceName == textBox1.Text)
        {
          if (service.Status != ServiceControllerStatus.Running)
          {
            service.Start();
          }
        }
      }
    }
  }
}

軟體截圖:

c# 將Minio.exe註冊成windows服務

以上就是c# 將Minio.exe註冊成windows服務的詳細內容,更多關於Minio.exe註冊成windows服務的資料請關注我們其它相關文章!