搜索 
 设为首页 加入收藏
当前位置>>首页-学院-.Net
来源: 作者: 日期:2009-07-03 12:37 网友评论:0条 点击:265
  C#获取WIN服务的路径和安装卸载服务

获取路径是通过注册表来查的

引用.

System.Configuration.Install.dll

System.ServiceProcess.dll

使用方法

安装


string _ServiceName = "";
string _InsertError = Zgke.WindowFrom.Service.ServiceSetup.InsertService(@"C:\Documents and Settings\Administrator\桌面\WindowsService1\WindowsService1\bin\Debug \WindowsService1.exe", ref _ServiceName);
if (_InsertError.Length == 0)
{
MessageBox.Show(_ServiceName, "安装成功!");
}
else
{
MessageBox.Show(_InsertError,"安装失败!");
}

卸载

Zgke.WindowFrom.Service.ServiceSetup.UnInsertService(@"C:\Documents and Settings\Administrator\桌面\WindowsService1\WindowsService1\bin\Debug \WindowsService1.exe");




显示服务列表

dataGridView1.DataSource = Zgke.WindowFrom.Service.ServiceSetup.GetServicesInfo();

下面是全部的类

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using System.Reflection;
using System.ServiceProcess;
using System.Configuration.Install;

using Microsoft.Win32;

namespace Zgke.WindowFrom.Service
{
///
/// 服务控制类
/// zgke@sina.com
/// qq:116149
///

public class ServiceSetup : ServiceController
{
public ServiceSetup(string p_Name)
: base(p_Name)
{

}

public string FilePath
{
get
{

RegistryKey _Key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\ControlSet001\Services\" + base.ServiceName);
if (_Key != null)
{
object _ObjPath = _Key.GetValue("ImagePath");
if (_ObjPath != null) return _ObjPath.ToString();
}
return "";
}
}

public string Description
{
get
{

RegistryKey _Key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\ControlSet001\Services\" + base.ServiceName);
if (_Key != null)
{
object _ObjPath = _Key.GetValue("Description");
if (_ObjPath != null) return _ObjPath.ToString();
}
return "";
}
}

///
/// 卸载服务
///

/// 指定服务文件路径
/// 卸载信息 正确卸载返回""
public static string UnInsertService(string p_Path)
{
if (!File.Exists(p_Path)) return "文件不存在!";
FileInfo _InsertFile = new FileInfo(p_Path);
IDictionary _SavedState = new Hashtable();
try
{
AssemblyInstaller _AssemblyInstaller = new AssemblyInstaller(p_Path, new string[] { "/LogFile=" + _InsertFile.DirectoryName + "\\" + _InsertFile.Name.Substring(0, _InsertFile.Name.Length - _InsertFile.Extension.Length) + ".log" });
_AssemblyInstaller.UseNewContext = true;
_AssemblyInstaller.Uninstall(_SavedState);
_AssemblyInstaller.Commit(_SavedState);
return "";
}
catch (Exception ex)
{
return ex.Message;
}
}

///
/// 安装服务
///

/// 指定服务文件路径
/// 返回安装完成后的服务名
/// 安装信息 正确安装返回""
public static string InsertService(string p_Path, ref string p_ServiceName)
{
if (!File.Exists(p_Path)) return "文件不存在!";
p_ServiceName = "";
FileInfo _InsertFile = new FileInfo(p_Path);
IDictionary _SavedState = new Hashtable();
try
{
AssemblyInstaller _AssemblyInstaller = new AssemblyInstaller(p_Path, new string[] { "/LogFile=" + _InsertFile.DirectoryName + "\\" + _InsertFile.Name.Substring(0, _InsertFile.Name.Length - _InsertFile.Extension.Length) + ".log" });
_AssemblyInstaller.UseNewContext = true;
_AssemblyInstaller.Install(_SavedState);
_AssemblyInstaller.Commit(_SavedState);
Type[] _TypeList = _AssemblyInstaller.Assembly.GetTypes();
for (int i = 0; i != _TypeList.Length; i++)
{
if (_TypeList[i].BaseType.FullName == "System.Configuration.Install.Installer")
{
object _InsertObject = System.Activator.CreateInstance(_TypeList[i]);
FieldInfo[] _FieldList = _TypeList[i].GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
for (int z = 0; z != _FieldList.Length; z++)
{
if (_FieldList[z].FieldType.FullName == "System.ServiceProcess.ServiceInstaller")
{
object _ServiceInsert = _FieldList[z].GetValue(_InsertObject);
if (_ServiceInsert != null)
{
p_ServiceName = ((ServiceInstaller)_ServiceInsert).ServiceName;
return "";
}
}
}
}
}
return "";
}
catch (Exception ex)
{
return ex.Message;
}
}

///
/// 获取服务指定列表(设备启动除外)
///

///
public static IList GetServicesInfo()
{
IList _List = new List();
ServiceController[] _ServiceList = ServiceController.GetServices();
for (int i = 0; i != _ServiceList.Length; i++)
{
_List.Add(new ServiceSetup(_ServiceList[i].ServiceName));
}
return _List;
}

///
/// 获取设备驱动服务
///

///
public static IList GetDevicesInfo()
{
IList _List = new List();
ServiceController[] _ServiceList = ServiceController.GetDevices();
for (int i = 0; i != _ServiceList.Length; i++)
{
_List.Add(new ServiceSetup(_ServiceList[i].ServiceName));
}
return _List;
}
}

}


【发表评论 0条】
发表评论
最新评论:已有0位感兴趣的读者发表了看法

验证码:请输入前面图中的四位验证码,字母不区分大小写


网站简介 | 广告服务 | VIP资费标准 | 银行汇款帐号 | 网站地图 | 帮助 | 联系方式
地址:成都八宝街一号万和苑C座1203 电话:028-86272612 传真: 028-86272612
开源人网站版权所有  渝ICP备06004507号  建议使用1024*768分辨率