C#获取WIN服务的路径和安装卸载服务

2009-9-3     作者:   转载自:java060515     编辑:乐乐   点击进入论坛

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

  引用.

  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;

  }

  }

  }

寻找产品:
姓       名: 电   话:
公       司: E-mail:
描       述: