public class ContractManager
{ IDictionary stateSaver = new Hashtable();
/// <summary>
/// 安装服务
/// </summary>
/// <param name="p_Path">指定服务文件路径</param>
/// <param name="p_ServiceName">返回安装完成后的服务名</param>
/// <returns>安装信息 正确安装返回""</returns>
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(, _InsertFile.Name.Length - _InsertFile.Extension.Length) + ".log" });
_AssemblyInstaller.UseNewContext = true;
_AssemblyInstaller.Install(_SavedState);
_AssemblyInstaller.Commit(_SavedState);
Type[] _TypeList = _AssemblyInstaller.Assembly.GetTypes();//获取安装程序集类型集合
for (int i = ; i != _TypeList.Length; i++)
{
if (_TypeList[i].BaseType.FullName == "System.Configuration.Install.Installer")
{
//找到System.Configuration.Install.Installer 类型
object _InsertObject = System.Activator.CreateInstance(_TypeList[i]);//创建类型实列
FieldInfo[] _FieldList = _TypeList[i].GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
for (int z = ; 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;
}
}
/// <summary>
/// 卸载服务
/// </summary>
/// <param name="filepath">服务路径</param>
public static void UnInstallService(string filepath, string ServiceName)
{ try
{ if (ServiceIsExisted(ServiceName))
{ //UnInstall Service AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller(); myAssemblyInstaller.UseNewContext = true; myAssemblyInstaller.Path = filepath; myAssemblyInstaller.Uninstall(null); myAssemblyInstaller.Dispose(); } } catch (Exception ex)
{
throw new Exception("unInstallServiceError\n" + ex.Message); } }
/// <summary>
/// 判断服务是否存在
/// </summary>
/// <param name="serviceName">服务名</param>
/// <returns></returns>
public static bool ServiceIsExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName == serviceName)
{
return true;
}
}
return false;
}
/// <summary>
/// 启动服务
/// </summary>
/// <param name="serviceName"></param>
/// <returns></returns>
public static bool ServiceStart(string serviceName)
{
try
{
ServiceController service = new ServiceController(serviceName);
if (service.Status == ServiceControllerStatus.Running)
{
return true;
}
else
{
TimeSpan timeout = TimeSpan.FromMilliseconds( * );
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
}
catch
{
return false;
}
return true;
} /// <summary>
/// 停止服务
/// </summary>
/// <param name="serviseName"></param>
/// <returns></returns>
public static bool ServiceStop(string serviseName)
{
try
{
ServiceController service = new ServiceController(serviseName);
if (service.Status == ServiceControllerStatus.Stopped)
{
return true;
}
else
{
TimeSpan timeout = TimeSpan.FromMilliseconds( * );
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
}
catch
{
return false;
}
return true;
} }

最新文章

  1. 救援linux
  2. IIS 服务没有及时响应启动或控制请求
  3. Maven使用笔记,错误Failure to Transfer后处理
  4. shell学习笔记(2)替换命令&#183;&#183;与()的区别
  5. [CODEVS1697]⑨要写信
  6. JQuery 解决 鼠标快速滑过后,会执行多次滑出的问题
  7. HDU 3362 Fix
  8. [置顶] 吃论扯谈---吃货和Office 365订阅的关系
  9. [Android]获取设备相关信息
  10. NET 分布式架构开发项目实战
  11. 201621123062《java程序设计》第五周作业总结
  12. echarts图表
  13. jQuery Validate和Thymeleaf相关
  14. Kaazing Gateway简单使用
  15. python实现关键词提取
  16. 000 Python的运行
  17. Maven依赖标红线,非jar包冲突问题
  18. sudo权限的设置
  19. .net图表之ECharts随笔05-不同01的语法步骤
  20. adb remount 失败:remount failed: Operation not permitted

热门文章

  1. ftp服务器PDF文件在线查看
  2. 数据库客户端工具Oracle SQL Developer
  3. 《Beginning Java 7》 - 4 - finalize() 手动垃圾回收
  4. IO模型《三》非阻塞IO
  5. PHP函数补完:call_user_func()
  6. jquery事件二 -- 选项卡,失去焦点
  7. html头文件设置常用之&lt;meta&gt;设置缓存
  8. mocha测试框架
  9. 启用mysql日志记录执行过的sql
  10. BellmanFord贝尔曼-福特算法