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

(0)

相关推荐

  • C# WindowsForm程序同时启动多个窗口类

    C# WindowsForm程序同时启动多个窗口类,具体内容如下 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace MVCProject { /// <summary> /// 多窗口同时启动类 /// <remarks>继承A

  • 使用C#创建Windows服务的实例代码

    本文介绍了使用C#创建Windows服务的实例代码,分享给大家 一.开发环境 操作系统:Windows 10 X64 开发环境:VS2015 编程语言:C# .NET版本:.NET Framework 4.0 目标平台:X86 二.创建Windows Service 1.新建一个Windows Service,并将项目名称改为"MyWindowsService",如下图所示: 2.在解决方案资源管理器内将Service1.cs改为MyService1.cs后并点击"查看代码&

  • C#编写Windows服务程序详细步骤详解(图文)

    一.创建一个Windows Service 1)创建Windows Service项目 2)对Service重命名 将Service1重命名为你服务名称,这里我们命名为ServiceTest. 二.创建服务安装程序 1)添加安装程序 之后我们可以看到上图,自动为我们创建了ProjectInstaller.cs以及2个安装的组件. 2)修改安装服务名 右键serviceInsraller1,选择属性,将ServiceName的值改为ServiceTest. 3)修改安装权限 右键servicePr

  • windows系统下,如何在C#程序中自动安装字体

    1.1.使用代码安装字体 注意:安装字体时,需要windows的管理员权限. [DllImport("kernel32.dll", SetLastError = true)] public static extern int WriteProfileString(string lpszSection, string lpszKeyName, string lpszString); [DllImport("gdi32")] public static extern i

  • c#实现windows远程桌面连接程序代码

    使用winform制作windows远程桌面连接程序,windows自带了远程桌面连接,我们需要将远程桌面连接集成 到自己的winform程序,并实现管理远程主机的配置. 远程桌面核心类库 windows系统自带了远程桌面activex dll,目录: c:\Windows\System32\mstscax.dll 此类库无法使用c#直接调用,介绍一个工具AxImp.exe AxImp.exe https://msdn.microsoft.com/zh-cn/library/8ccdh774(V

  • C# 监控 Windows 文件夹的方法

    您是否为无法看到孩子在电脑上存储的图片而发愁,您是否为无法监控员工在电脑上存储的东西而发愁,那么今天给您推荐的这款产品绝对是您不二的选择,它是由美国大厂生产,完全符合国际标准的产品,完美支持 Windows 98 以上系统,他就是 FileSystemWatcher 牌监控仪.他会侦听文件系统更改通知,并在目录或目录中的文件更改时引发事件.下面我们就来看看他的细节. 零.细节特征 1.常用的方法有: OnChanged(FileSystemEventArgs) 当更改被监控目录中文件或目录的大小

  • 利用C#修改Windows操作系统时间

    C#的System.DateTime类提供了对日期时间的封装,用它进行时间的转换和处理很方便,但是我没有在其中找到任何可以用来修改系统时间的成员.用过VC.VB等的朋友可能知道,我们可以调用Win32 API SetLocalTime来改变系统时间,看来C#中也只能如此了.SetLocalTime需要一个SYSTEMTIME结构指针作为参数,这倒不难,我们可以"比葫芦画瓢"很快在C#中定义这个结构,但问题是,我同时还想"享受".NET Framework的Syste

  • C#基于Windows服务的聊天程序(1)

    本文将演示怎么通过C#开发部署一个Windows服务,该服务提供各客户端的信息通讯,适用于局域网.采用TCP协议,单一服务器连接模式为一对多:多台服务器的情况下,当客户端连接数超过预设值时可自动进行负载转移,当然也可手动切换服务器,这种场景在实际项目中应用广泛. 简单的消息则通过服务器转发,文件类的消息则让客户端自己建立连接进行传输.后续功能将慢慢完善. 自定义协议: 1.新建Windows服务项目 2.修改配置文件添加 <appSettings> <add key="maxQ

  • C#实现Windows Form调用R进行绘图与显示的方法

    一.前提准备 安装R软件,需要安装32位的R软件,64位的调用会报错.另外就是讲R添加到电脑环境变量中. 打开R软件,安装包 scatterplot3d,演示需要用到此R包. 二.创建项目GraphGenerateByR,项目结构如下: 注意:这里需要引入RDotNet类库,可以自行下载:http://rdotnet.codeplex.com/ 三.Main窗体代码 using System; using System.Collections.Generic; using System.Comp

  • C# 屏蔽由于崩溃弹出的windows异常弹框

    windows应用程序(包括控制台)在运行时如果出现了未处理的异常会出项windows的异常提示框 这个提示框在平时并没有什么影响.但是当我们使用启动的是一个服务器程序时,我们的要求应该是尽可能快的重启应用. 但是由于这个提示框导致我们的第三方守护程序并不知道应用已经崩溃退出,导致我们无法及时处理. 所以,我们应该在程序启动时再做一个处理,即添加未处理异常的事件 C#:   AppDomain.CurrentDomain.UnhandledException 解释: 此事件提供通知未捕获的异常.

随机推荐