c# 将Minio.exe注册成windows服务
#代码知识 发布时间: 2026-01-12
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服务的详细内容,更多关于Minio.exe注册成windows服务的资料请关注其它相关文章!
代码知识SEO上一篇 : 基于.NET的FluentValidation数据验证实现
下一篇 : Python通过Schema实现数据验证方式
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!