저는 C#을 통해 몇 시간 동안 Windows 서비스를 설치하려고했습니다. 프로그래밍 방식으로 Windows 서비스를 설치할 수 없습니다.
은 내가InstallService()
기능, 심지어
InstallService()
실행 한 후 거짓
IsInstalled()
반환을 실행하면, 따라서 나는 창문 서비스를 시작할 수 없습니다입니다. 예를 들어
:
InstallService();
IsInstalled(); // false
ServiceBase[] ServicesToRun = new ServiceBase[] { new Service1() };
ServiceBase.Run(ServicesToRun); //Throws an exception because uninstalled!
그래서 여기, 난 단지 관련 코드를 보여주는있어 설치 코드입니다 :
private static void InstallService()
{
if (IsInstalled()) return;
try
{
using (AssemblyInstaller installer = GetInstaller())
{
IDictionary state = new Hashtable();
try
{
installer.Install(state);
installer.Commit(state);
}
catch
{
try
{
installer.Rollback(state);
}
catch { }
throw;
}
}
}
catch
{
throw;
}
}
private static AssemblyInstaller GetInstaller()
{
AssemblyInstaller installer = new AssemblyInstaller(
typeof(Service1).Assembly, null);
installer.UseNewContext = true;
return installer;
}
private static bool IsInstalled()
{
using (ServiceController controller =
new ServiceController("Service1"))
{
try
{
ServiceControllerStatus status = controller.Status;
}
catch
{
return false;
}
return true;
}
}
가 그리고 프로그램이 높은 권리를 실행? 그것은 정확히 어떻게 실패합니까? –