代码拉取完成,页面将自动刷新
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.ServiceProcess;
using System.Windows.Forms;
namespace Service
{
public partial class ServiceTool : Form
{
[DllImport("kernel32.dll")]
public static extern Boolean AllocConsole();
[DllImport("kernel32.dll")]
public static extern Boolean FreeConsole();
public ServiceTool()
{
InitializeComponent();
}
private void btnscan_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "可执行文件(*.exe)|*.exe";
if (ofd.ShowDialog() == DialogResult.OK)
{
txtpath.Text = ofd.FileName;
}
}
}
private void btnload_Click(object sender, EventArgs e)
{
//获得服务集合
var serviceControllers = ServiceController.GetServices();
cmbservices.DataSource = serviceControllers;
cmbservices.DisplayMember = "ServiceName";
cmbservices.ValueMember = "ServiceName";
}
private void btninstall_Click(object sender, EventArgs e)
{
var installPath = txtpath.Text;
if (string.IsNullOrEmpty(installPath))
{
MessageBox.Show("请选择要安装的服务程序");
btnscan.Focus();
return;
}
if (!File.Exists(installPath))
{
MessageBox.Show("文件不存在");
btnscan.Focus();
return;
}
string cmd = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe ";
using (Process p = new Process())
{
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.Start();//启动程序
p.StandardInput.AutoFlush = true;
p.StandardInput.WriteLine(cmd + installPath);
}
}
private void btnuninstall_Click(object sender, EventArgs e)
{
using (Process p = new Process())
{
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.Start();//启动程序
p.StandardInput.AutoFlush = true;
p.StandardInput.WriteLine("sc delete " + cmbservices.Text);
}
}
}
/// <summary>
/// 与控制台交互
/// </summary>
static class Shell
{
/// <summary>
/// 输出信息
/// </summary>
/// <param name="format"></param>
/// <param name="args"></param>
public static void WriteLine(string format, params object[] args)
{
WriteLine(string.Format(format, args));
}
/// <summary>
/// 输出信息
/// </summary>
/// <param name="output"></param>
public static void WriteLine(string output)
{
Console.ForegroundColor = GetConsoleColor(output);
Console.WriteLine(@"[{0}]{1}", DateTimeOffset.Now, output);
}
/// <summary>
/// 根据输出文本选择控制台文字颜色
/// </summary>
/// <param name="output"></param>
/// <returns></returns>
private static ConsoleColor GetConsoleColor(string output)
{
if (output.StartsWith("警告")) return ConsoleColor.Yellow;
if (output.StartsWith("错误")) return ConsoleColor.Red;
if (output.StartsWith("注意")) return ConsoleColor.Green;
return ConsoleColor.Gray;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。