我正在寻找一种方法来为我正在开发的Windows服务创建和/或添加安装程序。要求是不能有第三方材料(InstallShield,InstallUtil.exe,WiX等);它必须严格按照C#完成,我必须在安装服务之前将参数传递给服务。
我基本上只是编写一个服务来监视用户指定的文件夹及其子目录,以进行特定的更改。我已经建立了服务及其安装程序,但我需要能够将服务分发给客户'机器,我的服务唯一需要的是安装过程中的文件夹路径。
我已经搜索过所有搜索引擎优化和Goog,但我发现很少能帮助我构建自己的设置,所以如果有人可以帮我提供这方面的教程,或者链接到目前存在的关于如何在没有任何第三方内容的情况下构建自己的安装程序的完整教程,这将是非常棒的。
我的通用代码分为各自的文件:
的Program.cs:
using System.ServiceProcess;
namespace MyService
{
static class Program
{
static void Main( string[] args )
{
ServiceBase.Run( new MyService( args ) );
}
}
}
MyService.cs:
using System;
using System.IO;
using System.ServiceProcess;
namespace MyService
{
public partial class MyService : ServiceBase
{
public MyService( string[] args )
{
InitializeComponent();
this.ServiceName = "MyService";
this.AutoLog = this.CanHandlePowerEvent =
this.CanHandleSessionChangedEvent = this.CanPauseAndContinue = false;
this.CanStop = this.CanShutdown = true;
}
protected override void OnStart( string[] args )
{
FileSystemWatcher watcher = new FileSystemWatcher(/*'watcher' arguments*/)
{ IncludeSubdirectories = true /*, Set NotifyFilters*/};
watcher.Changed += ( sender, e ) => { /*Handle the event*/; };
watcher.EnableRaisingEvents = true;
base.OnStart( args );
}
protected override void OnStop()
{
watcher.Dispose();
base.OnStop();
}
protected override void OnShutdown()
{
watcher.Dispose();
base.OnShutdown();
}
}
}
ProjectInstaller.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
namespace MyService
{
[RunInstaller( true )]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
EventLogInstaller eventLogInstaller =
this.Installers.Cast<Installer>().First
( installer => installer is EventLogInstaller ) as EventLogInstaller;
if( eventLogInstaller != null )
eventLogInstaller.Log = "MyService";
this.Installers.Add( new ServiceProcessInstaller()
{ Username = null, Password = null } );
this.Installers.Add( new ServiceInstaller()
{ StartType = ServiceStartMode.Automatic } );
}
protected override void OnAfterInstall( IDictionary savedState )
{
base.OnAfterInstall( savedState );
using( ServiceController controller = new ServiceController
( this.serviceInstaller1.ServiceName, Environment.MachineName ) )
{ controller.Start(); }
}
}
}
我希望客户端能够双击安装文件,然后会显示一个窗口,询问他们希望服务观看的顶级目录路径。显然,我需要为目录浏览器功能构建一个GUI,我可以很容易地做到这一点,但我不知道如何将它们整合在一起。那么,有人可以告诉我如何将这三个文件或构建它们的可执行文件添加到安装程序中,以便我可以获取路径,将其传递给服务(我将从命令行执行,但我我已经知道如何做这个部分了,并在我的客户身上运行并运行它。机器?
答案 0 :(得分:0)
我已经构建了一些我们发送给最终用户的Windows服务。以下是架构
.bat
个文件
- 使用ServiceController sc.exe安装服务
- 使用相同的服务控制器启动/停止/卸载
醇>
用户必须解压缩交付物并右键单击蝙蝠并说“以管理员身份运行”。
您可以链接多个行,例如安装,启动和单个bat文件。
要查找/了解有关sc.exe的更多信息,请参阅here
样品:
sc create newservice binpath= c:\nt\system32\newserv.exe type= own start= auto depend= "+tdi netbios"