一切为了DotNet

念天地之悠悠,独怆然而涕下

 

在写windowsservice时的一点感受

        今天在写windowsservice时遇到了一个问题,主要是如果windowsservice名称已经存在,则不能进行注册,需要Installutil /u命令来卸载才能进行注册,但是如果不知道这个service的注册路径呢?如果是手动操作,可以直接到服务中去查找,我们就需要到注册表中去找了,service在注册表中的位置都在HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\[你的service名称]
在这个下面的ImagePath的键值就是我们要的路径了。
        在直接打开的ms-dos窗口是不能执行installutil命令的,必须打开VS2003自带的命令行才可以运行,为了找到原因,打开VS2003自带的命令行,也就是一个批处理文件才发现,需要手动给设置path,这样问题就解决了。按照这个批处理文件来建立一个sample.bat的批处理文件。
内容如下:
@SET VSINSTALLDIR=C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE
@SET VCINSTALLDIR=C:\Program Files\Microsoft Visual Studio .NET 2003
@SET FrameworkDir=C:\WINNT\Microsoft.NET\Framework
@SET FrameworkVersion=v1.1.4322
@SET FrameworkSDKDir=C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1
@SET SysDir=c:\WINNT\SYSTEM32
@rem Root of Visual Studio common files.

@if "%VSINSTALLDIR%"=="" goto Usage
@if "%VCINSTALLDIR%"=="" set VCINSTALLDIR=%VSINSTALLDIR%

@rem
@rem Root of Visual Studio ide installed files.
@rem
@set DevEnvDir=%VSINSTALLDIR%

@rem
@rem Root of Visual C++ installed files.
@rem
@set MSVCDir=%VCINSTALLDIR%\VC7

@rem
@echo setup the windows service of ECAService
@echo the path of the ECAService.exe is E:\work\ECAService\bin\Debug
@echo Now is running the command
@rem

@REM %VCINSTALLDIR%\Common7\Tools dir is added only for real setup.

@set PATH=%DevEnvDir%;%MSVCDir%\BIN;%VCINSTALLDIR%\Common7\Tools;%VCINSTALLDIR%\Common7\Tools\bin\prerelease;%VCINSTALLDIR%\Common7\Tools\bin;%FrameworkSDKDir%\bin;%FrameworkDir%\%FrameworkVersion%;%PATH%;%SysDir%
@set INCLUDE=%MSVCDir%\ATLMFC\INCLUDE;%MSVCDir%\INCLUDE;%MSVCDir%\PlatformSDK\include\prerelease;%MSVCDir%\PlatformSDK\include;%FrameworkSDKDir%\include;%INCLUDE%
@set LIB=%MSVCDir%\ATLMFC\LIB;%MSVCDir%\LIB;%MSVCDir%\PlatformSDK\lib\prerelease;%MSVCDir%\PlatformSDK\lib;%FrameworkSDKDir%\lib;%LIB%

@goto end

:end
建立一个控制台程序,代码如下:主要实现为新建一个setup.bat批处理,内容从sample.bat赋值过去,然后在setup.bat后面追加卸载和注册、启动service的命令。
最后执行这个批处理就一切ok了。

using System;
using Microsoft.Win32;
using System.IO;
namespace ECAInstall
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 class ECAInstall
 {
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
   //
   // TODO: 在此处添加代码以启动应用程序
   //
   string strFilePath ="";
   try
   {
    strFilePath=  Registry.LocalMachine.OpenSubKey("SYSTEM").OpenSubKey("ControlSet001").OpenSubKey("Services").OpenSubKey("ECAService").GetValue("ImagePath").ToString();
   }
   catch
   {
    strFilePath="";
   }
   
   //卸载ECAservice
   Console.WriteLine(strFilePath); 
   strFilePath = strFilePath.Replace("Program file","Progra~1");
   string strFileCreate = "setup.bat";
   string strFileExist = "sample.bat";
   StreamWriter sw = File.CreateText(strFileCreate);
    
   using (StreamReader sr = new StreamReader(strFileExist))
   {
    String line;
    // Read and display lines from the file until the end of
    // the file is reached.
    while ((line = sr.ReadLine()) != null)
    {
     sw.WriteLine(line);   
    }
   }    
   //sw.WriteLine(@"installutil E:\work\ECAService\bin\Debug\ecaservice.exe");
   if(strFilePath!="")
   {
    sw.WriteLine("installutil /u "+strFilePath);
   }
   //string strFileInstal = System.IO.Path.GetFullPath("ecaservice.exe");
   string strFileInstal = "ECAService.exe";
   sw.WriteLine(@"installutil "+strFileInstal);
   sw.WriteLine("net start ecaservice");
   sw.WriteLine("@echo off");
   sw.Close();   
 
   System.Diagnostics.Process  p=new  System.Diagnostics.Process(); 
   p.StartInfo.FileName="setup.bat"  ;//需要启动的程序名 
   p.Start();//启动 
  }
 }
}

 

posted on 2005-06-22 15:41  一切为了DotNet  阅读(911)  评论(2编辑  收藏  举报

导航