基本信息
源码名称:启动和停止window服务
源码大小:0.07M
文件格式:.zip
开发语言:C#
更新时间:2021-07-10
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace StarServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string thispath = Application.StartupPath;
public string Propath = "";
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "启动服务";
}
/// <summary>
/// 启动服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
string StarPath = @"%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe " Propath;
FileStream fs = new FileStream(thispath "\\Install.bat", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
try
{
sw.WriteLine(StarPath);
sw.WriteLine("Net Start ServiceTest");
sw.WriteLine("sc config ServiceTest start= auto");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
sw.Close();
fs.Close();
}
System.Diagnostics.Process.Start(thispath "\\Install.bat");
this.Text = "启动服务:你选择的服务已经启动。";
Cursor = Cursors.Default;
}
/// <summary>
/// 停止服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
string StarPath = @"%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u " Propath;
FileStream fs = new FileStream(thispath "\\Uninstall.bat", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
try
{
sw.WriteLine(StarPath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
sw.Close();
fs.Close();
}
System.Diagnostics.Process.Start(thispath "\\Uninstall.bat");
this.Text = "启动服务:你选择的服务已经卸载。";
Cursor = Cursors.Default;
}
private void button3_Click(object sender, EventArgs e)
{
///选择文件框 对象
OpenFileDialog ofd = new OpenFileDialog();
//打开时指定默认路径
ofd.InitialDirectory = @"C:\Documents and Settings\Administrator.ICBCOA-6E96E6BE\桌面";
//如果用户点击确定
if (ofd.ShowDialog() == DialogResult.OK)
{
//将用户选择的文件路径 显示 在文本框中
textBox1.Text = ofd.FileName;
Propath = textBox1.Text;
}
if (File.Exists(thispath "\\Uninstall.bat"))
{
File.Delete(thispath "\\Uninstall.bat");
}
File.Create(thispath "\\Uninstall.bat").Close();
if (File.Exists(thispath "\\Install.bat"))
{
File.Delete(thispath "\\Install.bat");
}
File.Create(thispath "\\Install.bat").Close();
}
//读写文本 - 写入数据按钮
private void buttonWrite_Click(string filePath)
{
}
/// <summary>
/// 运行CMD命令
/// </summary>
/// <param name="cmd">命令</param>
/// <returns></returns>
public static string Cmd(string[] cmd)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.AutoFlush = true;
for (int i = 0; i < cmd.Length; i )
{
p.StandardInput.WriteLine(cmd[i].ToString());
}
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
return strRst;
}
/// <summary>
/// 关闭进程
/// </summary>
/// <param name="ProcName">进程名称</param>
/// <returns></returns>
public static bool CloseProcess(string ProcName)
{
bool result = false;
System.Collections.ArrayList procList = new System.Collections.ArrayList();
string tempName = "";
int begpos;
int endpos;
foreach (System.Diagnostics.Process thisProc in System.Diagnostics.Process.GetProcesses())
{
tempName = thisProc.ToString();
begpos = tempName.IndexOf("(") 1;
endpos = tempName.IndexOf(")");
tempName = tempName.Substring(begpos, endpos - begpos);
procList.Add(tempName);
if (tempName == ProcName)
{
if (!thisProc.CloseMainWindow())
thisProc.Kill(); // 当发送关闭窗口命令无效时强行结束进程
result = true;
}
}
return result;
}
}
}
启动,服务
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.Text;
using System.Threading;
using System.Windows.Forms;
namespace StarServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string thispath = Application.StartupPath;
public string Propath = "";
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "启动服务";
}
/// <summary>
/// 启动服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
string StarPath = @"%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe " Propath;
FileStream fs = new FileStream(thispath "\\Install.bat", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
try
{
sw.WriteLine(StarPath);
sw.WriteLine("Net Start ServiceTest");
sw.WriteLine("sc config ServiceTest start= auto");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
sw.Close();
fs.Close();
}
System.Diagnostics.Process.Start(thispath "\\Install.bat");
this.Text = "启动服务:你选择的服务已经启动。";
Cursor = Cursors.Default;
}
/// <summary>
/// 停止服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
string StarPath = @"%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u " Propath;
FileStream fs = new FileStream(thispath "\\Uninstall.bat", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
try
{
sw.WriteLine(StarPath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
sw.Close();
fs.Close();
}
System.Diagnostics.Process.Start(thispath "\\Uninstall.bat");
this.Text = "启动服务:你选择的服务已经卸载。";
Cursor = Cursors.Default;
}
private void button3_Click(object sender, EventArgs e)
{
///选择文件框 对象
OpenFileDialog ofd = new OpenFileDialog();
//打开时指定默认路径
ofd.InitialDirectory = @"C:\Documents and Settings\Administrator.ICBCOA-6E96E6BE\桌面";
//如果用户点击确定
if (ofd.ShowDialog() == DialogResult.OK)
{
//将用户选择的文件路径 显示 在文本框中
textBox1.Text = ofd.FileName;
Propath = textBox1.Text;
}
if (File.Exists(thispath "\\Uninstall.bat"))
{
File.Delete(thispath "\\Uninstall.bat");
}
File.Create(thispath "\\Uninstall.bat").Close();
if (File.Exists(thispath "\\Install.bat"))
{
File.Delete(thispath "\\Install.bat");
}
File.Create(thispath "\\Install.bat").Close();
}
//读写文本 - 写入数据按钮
private void buttonWrite_Click(string filePath)
{
}
/// <summary>
/// 运行CMD命令
/// </summary>
/// <param name="cmd">命令</param>
/// <returns></returns>
public static string Cmd(string[] cmd)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.AutoFlush = true;
for (int i = 0; i < cmd.Length; i )
{
p.StandardInput.WriteLine(cmd[i].ToString());
}
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
return strRst;
}
/// <summary>
/// 关闭进程
/// </summary>
/// <param name="ProcName">进程名称</param>
/// <returns></returns>
public static bool CloseProcess(string ProcName)
{
bool result = false;
System.Collections.ArrayList procList = new System.Collections.ArrayList();
string tempName = "";
int begpos;
int endpos;
foreach (System.Diagnostics.Process thisProc in System.Diagnostics.Process.GetProcesses())
{
tempName = thisProc.ToString();
begpos = tempName.IndexOf("(") 1;
endpos = tempName.IndexOf(")");
tempName = tempName.Substring(begpos, endpos - begpos);
procList.Add(tempName);
if (tempName == ProcName)
{
if (!thisProc.CloseMainWindow())
thisProc.Kill(); // 当发送关闭窗口命令无效时强行结束进程
result = true;
}
}
return result;
}
}
}