栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

c# 将Minio.exe注册成windows服务

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 $PSscriptRootrnrn";
      content += "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12rn";
      content += "$config = @'rn";
      content += "rn";
      content += $" {textBox1.Text}rn";
      content += $" {textBox1.Text}rn";
      content += " MinIO server is a nb oss serverrn";
      content += " minio.exern";
      content += $@" " + "rn";
      content += $@" " + "rn";
      content += $@" server --address 0.0.0.0:{textBox2.Text} {textBox3.Text}" + "rn";
      content += @" rotate" + "rn";
      content += @" " + "rn";
      content += @"'@" + "rnrn";
      content += @"Set-Content ""minio-service.xml"" $config" + "rn";
      content += @"Start-Process -WorkingDirectory $PSscriptRoot -FilePath ""$($PSscriptRoot)minio-service.exe"" -ArgumentList ""install"" -NonewWindow -PassThru -Wait" + "rn";
      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);
    }

    /// 
    /// 启动服务
    /// 
    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服务的详细内容,更多关于Minio.exe注册成windows服务的资料请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/121711.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号