■ 目的
http://blogs.yahoo.co.jp/dk521123/31702889.htmlで、触れたが、Windowsサービスを作成するには、 VS Professional以上でないと、テンプレートなどがないので、作成できない。 が、VS Expressでも以下のサイトなどを参考にすれば、できそうなのでやってみた。http://space.geocities.jp/nequomame/dotnet/winservice/winservice_01.html
http://community.giga-works.com/windows/visualstudioexpress-editionwindows-c.html
http://kusakichi.sblo.jp/article/43601131.html
後日談
* Visual Studio Community 2017 だと簡単に作れる => 詳細は、以下の関連記事を参照。Windowsサービス ~ Hello World編 ~
https://blogs.yahoo.co.jp/dk521123/37948659.html
■ 環境
* VS 2010 Express (C#) * Windows XP
■ 作成手順
[1] メニュー[ファイル]-[新しいプロジェクト]を開く(プロジェクト名は「SampleWinSample」) [2] 「コンソールアプリケーション」を選択(何でもいいらしいが) [3] 参照追加で以下を追加しておく 3-1) System.Configuration.Install 3-2) System.ServiceProcess [4] プロジェクト名を右クリックして、[アプリケーション]タブを以下のように修正する 4-1) 出力の種類:Windowsアプリケーション 4-2) 対象のフレームワーク:.NET Framework 4 4-3) スタートアップ オブジェクト:SampleWinSample.Program [5] 以下のファイルを追加する(~.Designer.csも自分でファイル追加する) 5-1) Program.cs 5-2) SampleWindowsService.cs 5-3) SampleWindowsService.Designer.cs 5-4) SampleWindowsServiceInstaller.cs 5-5) SampleWindowsServiceInstaller.Designer.cs
サンプル
Program.cs
using System.ServiceProcess;
namespace SampleWinSample
{
class Program
{
static void Main(string[] args)
{
ServiceBase[] servicesToRun =
new ServiceBase[]
{
new SampleWindowsService(),
};
ServiceBase.Run(servicesToRun);
}
}
}
SampleWindowsService.cs
using System.ServiceProcess;
namespace SampleWinSample
{
public partial class SampleWindowsService : ServiceBase
{
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
}
}
SampleWindowsService.Designer.cs
using System.Diagnostics;
using System.ServiceProcess;
namespace SampleWinSample
{
public partial class SampleWindowsService : ServiceBase
{
// コンポーネント デザイナで必要です。
private System.ComponentModel.IContainer components;
[DebuggerNonUserCode()]
protected override void Dispose(bool disposing)
{
if (disposing && this.components != null)
{
this.components.Dispose();
}
base.Dispose(disposing);
}
[DebuggerStepThrough()]
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
// SampleWindowsService
this.AutoLog = false;
this.ServiceName = "SampleWindowsService";
}
}
}
SampleWindowsServiceInstaller.cs
using System.ComponentModel;
using System.Configuration.Install;
namespace SampleWinSample
{
[RunInstallerAttribute(true)]
public partial class SampleWindowsServiceInstaller : Installer
{
public SampleWindowsServiceInstaller()
{
this.InitializeComponent();
}
}
}
SampleWindowsServiceInstaller.Designer.cs
using System.Configuration.Install;
using System.Diagnostics;
using System.ServiceProcess;
namespace SampleWinSample
{
public partial class SampleWindowsServiceInstaller : Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller serviceProcessInstaller;
[DebuggerStepThrough()]
private void InitializeComponent()
{
this.serviceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller
//
this.serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller.Password = null;
this.serviceProcessInstaller.Username = null;
//
// serviceInstaller
//
this.serviceInstaller.ServiceName = "SampleWindowsService";
this.serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
//
// SampleWindowsServiceInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller,
this.serviceInstaller});
}
}
}
関連記事
Windowsサービス
Windowsサービス ~ 基本編 ~https://blogs.yahoo.co.jp/dk521123/31702889.html
Windowsサービス ~ Hello World編 ~
https://blogs.yahoo.co.jp/dk521123/37948659.html
SCコマンド ~ サービスの制御 ~
https://blogs.yahoo.co.jp/dk521123/29631029.html
VS Express で Windowsサービス のテンプレート作成
https://blogs.yahoo.co.jp/dk521123/31706797.html
ServiceControllerクラス ~サービスをコントロールする~
https://blogs.yahoo.co.jp/dk521123/31737290.html
Windwsサービスに関するトラブルシューティング
https://blogs.yahoo.co.jp/dk521123/14491769.html