欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-24 21:58:14
minio 注册成windows 服务的工具开发using system;using system.collections.generic;using system.componentmodel;us...

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服务的资料请关注其它相关文章!