C#程序安装.net服务(不使用InstallUtil.exe)
来源:互联网 发布:html5纯静态源码 编辑:程序博客网 时间:2024/06/04 19:51
摘自:http://blog.csdn.net/lembo520390/archive/2009/06/02/4236463.aspx
前面写了通常的写.net服务 的方法。 其实也可以不用该工具来安装服务,比如在一个Winform中点一个按钮来实现安装。 思路其实很简单,你通过 System.Configuration.Install.ManagedInstallerClass 类中的静态方法 InstallHelper就可以实现手工安装。 该方法的签名如下:
public static void InstallHelper(string[] args)
其中参数 args 就是你用 InstallUtil.exe 工具安装时的参数。一般就是一个exe的文件名。
实现的步骤如下:
前3步和 前面写的一样,可以参考这里 http://blog.csdn.net/lembo520390/archive/2009/06/02/4236379.aspx
接着你可以新建一个winform的项目,在上面放一个按钮,按钮的点击事件代码可以这样:
view plaincopy to clipboardprint?
/// <summary>
/// 安装服务
/// </summary>
private void btnInstall_Click(object sender, EventArgs e)
{
string[] args = { "myService.exe"};
ServiceController sCtr = new ServiceController(serviceName);
if (!ServiceIsExisted(serviceName))
{
try
{
ManagedInstallerClass.InstallHelper(args);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
else
{
MessageBox.Show("该服务已经存在,不用重复安装。");
}
}
/// <summary>
/// 安装服务
/// </summary>
private void btnInstall_Click(object sender, EventArgs e)
{
string[] args = { "myService.exe"};
ServiceController sCtr = new ServiceController(serviceName);
if (!ServiceIsExisted(serviceName))
{
try
{
ManagedInstallerClass.InstallHelper(args);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
else
{
MessageBox.Show("该服务已经存在,不用重复安装。");
}
}
其中ServiceIsExisted 是判断服务是否存在的方法,代码如下:
view plaincopy to clipboardprint?
/// <summary>
/// 检查指定的服务是否存在。
/// </summary>
/// <param name="serviceName">要查找的服务名字</param>
/// <returns></returns>
private bool ServiceIsExisted(string svcName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName == svcName)
{
return true;
}
}
return false;
}
/// <summary>
/// 检查指定的服务是否存在。
/// </summary>
/// <param name="serviceName">要查找的服务名字</param>
/// <returns></returns>
private bool ServiceIsExisted(string svcName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName == svcName)
{
return true;
}
}
return false;
}
然后把前面生成的myService.exe和这个winform生成的可执行文件放到同一个目录下,
运行winform点按钮就可实现和运行 InstallUtil.exe 工具一样的效果。
其实 InstallUtil.exe内部也是调用了ManagedInstallerClass.InstallHelper方法。 自己Reflector一下就知道了。:)
- C#程序安装.net服务(不使用InstallUtil.exe)
- C#程序安装.net服务(不使用InstallUtil.exe)
- 程序安装.net服务(不使用InstallUtil.exe)
- 程序安装.net服务(不使用InstallUtil.exe)
- [转]C#程序安装.net服务(不使用InstallUtil.exe)
- 不使用installutil.exe安装WINDOWS服务
- 不使用installutil.exe安装WINDOWS服务
- 不使用installutil.exe安装WINDOWS服务
- 不使用installutil.exe安装WINDOWS服务
- 不使用installutil.exe安装WINDOWS服务.
- 不使用installutil.exe安装WINDOWS服务
- 不使用installutil.exe安装WINDOWS服务
- 使用程序代码安装/卸载.net服务(不使用InstallUtil.exe)
- 使用程序代码安装/卸载.net服务(不使用InstallUtil.exe)
- InstallUtil.exe 安装服务程序
- 转:使用C#代码安装 Windows 服务(不使用InstallUtil)
- 使用C#代码安装 Windows 服务(不使用InstallUtil) (转帖)
- 使用C#代码安装 Windows 服务(不使用InstallUtil)
- Android系统笔记
- sencha cmd 常用命令
- 关于socket阻塞与非阻塞情况下的recv、send、read、write返回值
- log4j 配置问题
- Select系统调用方法
- C#程序安装.net服务(不使用InstallUtil.exe)
- Android自动化与持续集成实践
- How to open and close wallet for Oracle Databse 11g?
- 红警2 Win7启动一系列问题
- 求两个排好序的数组中的第k小数字
- C# 定时执行1
- Android Developers:绘制9-patch图片
- 香港美食攻略(3)——源记喳咋
- oracle数据库删除触发器实例