3 Star 1 Fork 1

Gui.H/WindowsSeviceTool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ServiceTool.cs 4.23 KB
一键复制 编辑 原始数据 按行查看 历史
Gui.H 提交于 2017-12-27 15:07 . InitializeComponent
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;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/SpringHgui/WindowsSeviceTool.git
git@gitee.com:SpringHgui/WindowsSeviceTool.git
SpringHgui
WindowsSeviceTool
WindowsSeviceTool
master

搜索帮助