自安装Windows服务在.NET C# [英] Self install windows service in .NET c#
问题描述
我已经阅读了这个问题创新安装为Windows服务? 。我有同样的问题,但我不understant从lubos hasko答案。我究竟该怎么办呢?你能有人张贴我充满演练?
当我在下面运行的代码,一些已安装,但在服务列表中,我找不到它。
我有这个,但是这不工作:
使用系统;
使用System.Collections.Generic;
使用System.Configuration.Install;
使用System.Linq的;
使用的System.Reflection;
使用System.ServiceProcess;
使用System.Text;
:使用System.IO;
命名ConsoleApplication1
{
公共类服务1:ServiceBase
{
公共服务1()
{
File.AppendAllText(sss.txt,CCCCC);
}
保护覆盖无效的OnStart(字串[] args)
{
File.AppendAllText(sss.txt,asdfasdf);
}
保护覆盖无效调用OnStop()
{
File.AppendAllText(sss.txt,BBBBB);
}
静态无效的主要(字串[] args)
{
如果(System.Environment.UserInteractive)
{
字符串参数= string.Concat(参数);
开关(参数)
{
案--install:
ManagedInstallerClass.InstallHelper(。新的String [] {Assembly.GetExecutingAssembly()位置});
中断;
案--uninstall:
ManagedInstallerClass.InstallHelper(新的String [] {/ U,Assembly.GetExecutingAssembly()位置。});
中断;
}
}
,否则
{
ServiceBase.Run(新服务1());
}
Console.ReadKey();
}
}
}
我不understad这两种:
如果(System.Environment.UserInteractive)...
这是我的完整的解决方案,它的作品。这是基本相同的答案作为这个问题
使用系统;
使用System.Configuration.Install;
使用的System.Reflection;
使用System.ServiceProcess;
:使用System.IO;
命名ConsoleApplication1
{
类项目:ServiceBase
{
静态无效的主要(字串[] args)
{
AppDomain.CurrentDomain.UnhandledException + = CurrentDomainUnhandledException;
如果(System.Environment.UserInteractive)
{
字符串参数= string.Concat(参数);
开关(参数)
{
案--install:
ManagedInstallerClass.InstallHelper(。新的String [] {Assembly.GetExecutingAssembly()位置});
中断;
案--uninstall:
ManagedInstallerClass.InstallHelper(新的String [] {/ U,Assembly.GetExecutingAssembly()位置。});
中断;
}
}
,否则
{
ServiceBase.Run(新计划());
}
}
私有静态无效CurrentDomainUnhandledException(对象发件人,UnhandledExceptionEventArgs E)
{
File.AppendAllText(@C:\Temp\error.txt((例外)e.ExceptionObject).Message +((例外)e.ExceptionObject).InnerException.Message);
}
公共计划()
{
this.ServiceName =我的服务;
File.AppendAllText(@C:\Temp\sss.txt,AAA);
}
保护覆盖无效的OnStart(字串[] args)
{
base.OnStart(参数);
File.AppendAllText(@C:\Temp\sss.txt,BBB);
}
保护覆盖无效调用OnStop()
{
base.OnStop();
File.AppendAllText(@C:\Temp\sss.txt,CCC);
}
}
}
和在同一个项目中创建这个类:
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Configuration.Install;
使用System.Linq的;
使用System.ServiceProcess;
使用System.Text;
命名ConsoleApplication1
{
[runInstaller的(真)]
公共类MyWindowsServiceInstaller:安装
{
公共MyWindowsServiceInstaller()
{
变种processInstaller =新ServiceProcessInstaller();
变种的ServiceInstaller =新的ServiceInstaller();
//设置权限
processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.DisplayName =我的服务;
serviceInstaller.StartType = ServiceStartMode.Automatic;
//必须是相同的东西是在程序的构造函数中
serviceInstaller.ServiceName =我的服务中设置;
this.Installers.Add(processInstaller);
this.Installers.Add(的ServiceInstaller);
}
}
}
运行此程序的参数 - -install / - 卸载作为Windows 7.检查错误日志中的临时管理。检查同一路径上工作日志。
I have read this question Inno Setup for Windows service?. I have same issue, but I dont understant the answer from lubos hasko. How exactly can I do it? Can you someone post me full walkthrough?
When I run code below, something is installed, but in list of service, I could not find it. I have this, but this not work:
using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
public class Service1 : ServiceBase
{
public Service1()
{
File.AppendAllText("sss.txt", "ccccc");
}
protected override void OnStart(string[] args)
{
File.AppendAllText("sss.txt", "asdfasdf");
}
protected override void OnStop()
{
File.AppendAllText("sss.txt", "bbbbb");
}
static void Main(string[] args)
{
if (System.Environment.UserInteractive)
{
string parameter = string.Concat(args);
switch (parameter)
{
case "--install":
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
break;
case "--uninstall":
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
}
}
else
{
ServiceBase.Run(new Service1());
}
Console.ReadKey();
}
}
}
I dont understad this either:
if (System.Environment.UserInteractive) ...
This is my complete solution, and it works. It is basically the same answer as in this question.
using System;
using System.Configuration.Install;
using System.Reflection;
using System.ServiceProcess;
using System.IO;
namespace ConsoleApplication1
{
class Program : ServiceBase
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
if (System.Environment.UserInteractive)
{
string parameter = string.Concat(args);
switch (parameter)
{
case "--install":
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
break;
case "--uninstall":
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
}
}
else
{
ServiceBase.Run(new Program());
}
}
private static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
File.AppendAllText(@"C:\Temp\error.txt", ((Exception)e.ExceptionObject).Message + ((Exception)e.ExceptionObject).InnerException.Message);
}
public Program()
{
this.ServiceName = "My Service";
File.AppendAllText(@"C:\Temp\sss.txt", "aaa");
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
File.AppendAllText(@"C:\Temp\sss.txt", "bbb");
}
protected override void OnStop()
{
base.OnStop();
File.AppendAllText(@"C:\Temp\sss.txt", "ccc");
}
}
}
and in same project create this class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace ConsoleApplication1
{
[RunInstaller(true)]
public class MyWindowsServiceInstaller : Installer
{
public MyWindowsServiceInstaller()
{
var processInstaller = new ServiceProcessInstaller();
var serviceInstaller = new ServiceInstaller();
//set the privileges
processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.DisplayName = "My Service";
serviceInstaller.StartType = ServiceStartMode.Automatic;
//must be the same as what was set in Program's constructor
serviceInstaller.ServiceName = "My Service";
this.Installers.Add(processInstaller);
this.Installers.Add(serviceInstaller);
}
}
}
Run this program with parameters --install/--uninstall as Administrator on Windows 7. Check the error log in temp. Check working log on the same path.
这篇关于自安装Windows服务在.NET C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!