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

作者:easten 时间:2022-09-25 20:51:18 

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服务

来源:https://www.cnblogs.com/dongteng/p/13955178.html

标签:c#,minio,windows服务
0
投稿

猜你喜欢

  • spring AOP定义AfterThrowing增加处理实例分析

    2021-07-11 14:22:11
  • MyBatis的 config.xml标签

    2021-07-18 02:01:34
  • java中struts2实现文件上传下载功能实例解析

    2022-03-31 06:47:38
  • 谈谈为JAXB和response设置编码,解决wechat4j中文乱码的问题

    2023-07-31 01:34:27
  • java中关于深拷贝的几种方式总结

    2023-12-13 17:39:41
  • Java集合删除元素ArrayList实例详解

    2022-11-09 19:12:39
  • javaweb实现在线支付功能

    2022-02-08 19:17:34
  • android实现通过NFC读取卡号

    2023-06-24 08:45:54
  • 基于Java编写串口通信工具

    2022-11-30 09:25:34
  • java实现仿射密码加密解密

    2022-10-09 04:04:49
  • Android选择图片或拍照图片上传到服务器

    2022-12-15 01:48:41
  • Java数据结构之稀疏数组的实现与应用

    2023-12-04 22:46:41
  • SpringCloud Feign配置应用详细介绍

    2023-07-14 04:23:03
  • IDEA2020.1个性化设置的实现

    2023-10-17 09:31:34
  • java加载properties文件的六种方法总结

    2023-09-20 05:24:54
  • C#实现泛型List分组输出元素的方法

    2022-03-10 07:33:15
  • slf4j jcl jul log4j1 log4j2 logback各组件系统日志切换

    2023-08-08 13:00:41
  • springboot整合vue实现上传下载文件

    2023-11-14 07:10:37
  • SpringCloud之Config配置中心与Redis分布式锁详解

    2023-12-21 02:14:41
  • c#实现选择排序的示例

    2021-10-15 00:32:27
  • asp之家 软件编程 m.aspxhome.com