进入东财以来,看到一个比较好的手动安装删除服务的,使用的是PINVOKE CreateService DeleteService的方法,不用使用vs安装目录下的installutil工具来做,自定义很多命令行参数,先看这个被调用函数
public class InstallerCommon
{
[DllImport("advapi32.dll")]
public static extern void CloseServiceHandle(IntPtr SCHANDLE);
[DllImport("Advapi32.dll")]
public static extern IntPtr CreateService(IntPtr SC_HANDLE, string lpSvcName, string lpDisplayName, int dwDesiredAccess, int dwServiceType, int dwStartType, int dwErrorControl, string lpPathName, string lpLoadOrderGroup, int lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword);
[DllImport("advapi32.dll")]
public static extern int DeleteService(IntPtr SVHANDLE);
[DllImport("kernel32.dll")]
public static extern int GetLastError();
public bool Install(string svcPath, string svcName, string svcDispName, string svcDescription = "")
{
bool flag = false;
int scParameter = 2;
int dwServiceType = 0x10;
int dwErrorControl = 1;
int num4 = 0xf0000;
int num5 = 1;
int num6 = 2;
int num7 = 4;
int num8 = 8;
int num9 = 0x10;
int num10 = 0x20;
int num11 = 0x40;
int num12 = 0x80;
int num13 = 0x100;
int dwDesiredAccess = ((((((((num4 | num5) | num6) | num7) | num8) | num9) | num10) | num11) | num12) | num13;
int dwStartType = 2;
try
{
IntPtr ptr = OpenSCManager(null, null, scParameter);
if (ptr.ToInt32() != 0)
{
if (CreateService(ptr, svcName, svcDispName, dwDesiredAccess, dwServiceType, dwStartType, dwErrorControl, svcPath, null, 0, null, null, null).ToInt32() == 0)
{
flag = false;
Console.Write("服务安装失败!");
}
else
{
flag = true;
}
CloseServiceHandle(ptr);
}
}
catch (Exception exception)
{
throw exception;
}
if (flag)
{
RegistryKey system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
RegistryKey currentControlSet = system.OpenSubKey("CurrentControlSet");
RegistryKey services = currentControlSet.OpenSubKey("Services");
RegistryKey service = services.OpenSubKey(svcName, true);
service.SetValue("Description", svcDescription);
}
Console.Write("服务安装成功!");
return flag;
}
[DllImport("advapi32.dll")]
public static extern IntPtr OpenSCManager(string lpMachineName, string lpSCDB, int scParameter);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern IntPtr OpenService(IntPtr SCHANDLE, string lpSvcName, int dwNumServiceArgs);
[DllImport("advapi32.dll")]
public static extern int StartService(IntPtr SVHANDLE, int dwNumServiceArgs, string lpServiceArgVectors);
public bool UnInstall(string svcName)
{
int scParameter = 0x40000000;
IntPtr sCHANDLE = OpenSCManager(null, null, scParameter);
if (sCHANDLE.ToInt32() != 0)
{
int dwNumServiceArgs = 0x10000;
IntPtr sVHANDLE = OpenService(sCHANDLE, svcName, dwNumServiceArgs);
if (sVHANDLE.ToInt32() != 0)
{
if (DeleteService(sVHANDLE) != 0)
{
CloseServiceHandle(sCHANDLE);
return true;
}
CloseServiceHandle(sCHANDLE);
return false;
}
return false;
}
return false;
}
}
然后在program.cs写命令行参数逻辑:
static void Main(string[] args)
{
if (args.Length >= 2)
{
var server = args[1];
InstallerCommon iS = new InstallerCommon();
if (args[0] == "add")
{
string description = "";
if (args.Length == 3)
description = args[2];
iS.Install(System.Reflection.Assembly.GetExecutingAssembly().Location, server, server, description);
}
if (args[0] == "del")
iS.UnInstall(server);
}
else
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new FundSearchService()
};
ServiceBase.Run(ServicesToRun);
}
}
还是很好用的。