实例介绍
【实例简介】
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.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;
}
}
}
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论