用C#代码来安装、卸载、启动、关闭服务
来源:互联网 发布:视频音乐录制软件 编辑:程序博客网 时间:2024/05/16 11:27
//// <summary>
/// 启动服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
ServiceController sc = new ServiceController("WindowsService1");
if (sc.Status.Equals(ServiceControllerStatus.Stopped))
{
sc.Start();
}
}
/**//// <summary>
/// 停止服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
ServiceController sc = new ServiceController("MSSQLSERVER");
if (!sc.Status.Equals(ServiceControllerStatus.Stopped))
{
sc.Stop();
}
}
/**//// <summary>
/// 安装服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
if (!isServiceIsExisted("Service1"))
{
string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
string serviceFileName = location.Substring(0, location.LastIndexOf('\\') + 1) + "WindowsService1.exe";
InstallmyService(null, serviceFileName);
}
else
{
MessageBox.Show("系统已经安装了此服务!");
}
}
/**//// <summary>
/// 卸载服务
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
if (isServiceIsExisted("Service1"))
{
string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
string serviceFileName = location.Substring(0, location.LastIndexOf('\\') + 1) + "WindowsService1.exe";
UnInstallmyService(serviceFileName);
}
else
{
MessageBox.Show("系统不存在此服务,不需要卸载!");
}
}
/**//// <summary>
/// 检查服务存在的存在性
/// </summary>
/// <param name=" NameService ">服务名</param>
/// <returns>存在返回 true,否则返回 false;</returns>
public static bool isServiceIsExisted(string NameService)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName.ToLower() == NameService.ToLower())
{
return true;
}
}
return false;
}
/**//// <summary>
/// 安装Windows服务
/// </summary>
/// <param name="stateSaver">集合</param>
/// <param name="filepath">程序文件路径</param>
public static void InstallmyService(IDictionary stateSaver, string filepath)
{
AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
AssemblyInstaller1.UseNewContext = true;
AssemblyInstaller1.Path = filepath;
AssemblyInstaller1.Install(stateSaver);
AssemblyInstaller1.Commit(stateSaver);
AssemblyInstaller1.Dispose();
}
/**//// <summary>
/// 卸载Windows服务
/// </summary>
/// <param name="filepath">程序文件路径</param>
public static void UnInstallmyService(string filepath)
{
AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
AssemblyInstaller1.UseNewContext = true;
AssemblyInstaller1.Path = filepath;
AssemblyInstaller1.Uninstall(null);
AssemblyInstaller1.Dispose();
}
- 用C#代码来安装、卸载、启动、关闭服务
- 用C#代码来安装、卸载、启动、关闭服务
- 用C#代码来安装、卸载、启动、关闭服务
- C#启用服务 关闭服务 安装服务 卸载服务
- C#启用服务 关闭服务 安装服务 卸载服务 收藏
- C#启用服务 关闭服务 安装服务 卸载服务
- C#启用服务 关闭服务 安装服务 卸载服务 .
- C# Windows服务安装、卸载批处理代码
- C# Windows服务安装、卸载批处理代码
- C#安装卸载服务
- C#对Windows服务操作(注册安装服务,卸载服务,启动停止服务,判断服务存在)
- windows服务(安装/启动/卸载/停止)批处理代码
- Windows服务代码控制安装卸载、启动停止
- 用winform项目来执行服务的安装与卸载
- C#操作注册服务卸载服务启动服务停止服务..
- C#操作注册服务卸载服务启动服务停止服务.. .
- C# 远程服务器 安装、卸载 Windows 服务,读取远程注册表,关闭杀掉远程进程
- 浅谈C#安装服务和卸载服务
- 通过SvcUtil.exe生成客户端代码和配置
- 针对unix环境编程的一些提问!
- ace支持epoll编译方法
- javascript开发经验总结【一】:多重条件判断
- ListView组件单击无效问题
- 用C#代码来安装、卸载、启动、关闭服务
- java.sql.SQLException: 无效的列索引
- GridView 自带分页
- 搜狗的笔试题
- POJ 3449 Geometric Shapes (线段相交)
- OpenCV学习笔记(24)CLR模式下System::Drawing::Bitmap与cv::Mat图像格式的转换
- android 读取电话号码和联系人
- js模版深度解析
- eclipse中中文乱码