服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - c# 将Minio.exe注册成windows服务

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

2022-10-14 12:14easten C#

这篇文章主要介绍了c# 如何将Minio.exe注册成windows服务,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下

minio 注册成windows 服务的工具开发

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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服务的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/dongteng/p/13955178.html

延伸 · 阅读

精彩推荐
  • C#C# 读取ttf字体文件里的Unicode实现

    C# 读取ttf字体文件里的Unicode实现

    这篇文章主要介绍了C# 读取 ttf字体文件里的 Unicode实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

    超蓝微猫10292022-10-09
  • C#C# task应用实例详解

    C# task应用实例详解

    这篇文章主要介绍了如何在C#中一些应用task的实例,简单易懂的代码能更好的帮你学习,有兴趣的朋友可以了解下...

    Kiba5185492022-09-08
  • C#C#的Excel导入、导出

    C#的Excel导入、导出

    这篇文章主要为大家详细介绍了C#的Excel导入、导出的相关资料,需要的朋友可以参考下...

    polk65252021-11-22
  • C#c# 委托的本质是什么

    c# 委托的本质是什么

    这篇文章主要介绍了c# 委托的本质是什么,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...

    Learning hard11932022-09-28
  • C#C#使用正则表达式实现首字母转大写的方法

    C#使用正则表达式实现首字母转大写的方法

    这篇文章主要介绍了C#使用正则表达式实现首字母转大写的方法,涉及C#基于正则表达式操作字符串的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    宁静.致远7652021-11-02
  • C#C#使用TensorFlow.NET训练自己的数据集的方法

    C#使用TensorFlow.NET训练自己的数据集的方法

    这篇文章主要介绍了C#使用TensorFlow.NET训练自己的数据集的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    pepure12062022-08-30
  • C#基于C#实现微信支付宝扫码支付功能

    基于C#实现微信支付宝扫码支付功能

    为公司系统业务需要,这几天了解了一下微信和支付宝扫码支付的接口,并用c#实现了微信和支付宝扫码支付的功能。需要的朋友跟随小编一起看看吧...

    八神太兮8722022-07-28
  • C#Unity实现3D循环滚动效果

    Unity实现3D循环滚动效果

    这篇文章主要为大家详细介绍了Unity实现3D循环滚动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    海涛高软9532022-03-11