110,534
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 注册服务
/// </summary>
/// <param name="strServiceName"></param>
/// <param name="strServiceInstallPath"></param>
public void Register(string servicesname, string strServiceInstallPath)
{
IDictionary mySavedState = new Hashtable();
try
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(servicesname);
//服务已经存在则卸载
if (ServiceIsExisted(servicesname))
{
UnInstallService(servicesname, strCurrPath);
}
service.Refresh();
//注册服务
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
mySavedState.Clear();
//myAssemblyInstaller.Path = strCurrPath;
myAssemblyInstaller.Path = "WSGPSServices.exe";
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Install(mySavedState);
myAssemblyInstaller.Commit(mySavedState);
myAssemblyInstaller.Dispose();
service.Start();
ProPro();
}
catch (Exception ex)
{
throw new Exception("注册服务时出错:" + ex.Message);
}
}
/// <summary>
/// 启动服务程序
/// </summary>
public void ProPro()
{ System.Diagnostics.Process pro = new Process();
pro.StartInfo.FileName = @"D:\study\NewGPS\WSGPSGateway\WSGPSServices\bin\Release\WSGPSGateway.exe";
//pro.StartInfo.WorkingDirectory = "'D:'+'\'study'+'\'NewGPS'+'\'WSGPSGateway'+'\'WSGPSServices'+'\'bin'+'\'Release'";
//pro.StartInfo.Arguments = "D:\\study\\NewGPS\\WSGPSGateway\\WSGPSServices\\bin\\Release";
pro.StartInfo.CreateNoWindow = true;
try
{
pro.Start();
}
catch (Exception ex)
{
throw ex;
}
}