Visual Studio セットアップ プロジェクトからインストールを実行した後、サービスを自動的に開始するにはどうすればよいですか?
私はこれを理解し、一般的な利益のために答えを共有すると思いました. 従うべき答え。私はこれを行うための他のより良い方法を受け入れています。
Visual Studio セットアップ プロジェクトからインストールを実行した後、サービスを自動的に開始するにはどうすればよいですか?
私はこれを理解し、一般的な利益のために答えを共有すると思いました. 従うべき答え。私はこれを行うための他のより良い方法を受け入れています。
次のクラスをプロジェクトに追加します。
using System.ServiceProcess;
class ServInstaller : ServiceInstaller
{
protected override void OnCommitted(System.Collections.IDictionary savedState)
{
ServiceController sc = new ServiceController("YourServiceNameGoesHere");
sc.Start();
}
}
セットアップ プロジェクトはクラスを取得し、インストーラーの終了後にサービスを実行します。
このアプローチでは、Installer クラスと最小量のコードを使用します。
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace MyProject
{
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
serviceInstaller1.AfterInstall += (sender, args) => new ServiceController(serviceInstaller1.ServiceName).Start();
}
}
}
serviceInstaller1
インストーラー クラス デザイナーで定義(ServiceInstaller 型) し、デザイナーでそのServiceName
プロパティも設定します。
実行してくれてありがとう...
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
ServiceController sc = new ServiceController("YourServiceName");
sc.Start();
}
独自のクラスを作成する代わりに、プロジェクト インストーラーでサービス インストーラーを選択し、Committed イベントにイベント ハンドラーを追加します。
private void serviceInstallerService1_Committed(object sender, InstallEventArgs e)
{
var serviceInstaller = sender as ServiceInstaller;
// Start the service after it is installed.
if (serviceInstaller != null && serviceInstaller.StartType == ServiceStartMode.Automatic)
{
var serviceController = new ServiceController(serviceInstaller.ServiceName);
serviceController.Start();
}
}
スタートアップの種類が自動に設定されている場合にのみ、サービスが開始されます。
上記のスニペットに基づくと、私のProjectInstaller.csファイルは、FSWServiceMgr.exeという名前のサービスに対して次のようになりました。インストール後にサービスが開始されました。補足として、ソリューションエクスプローラーでセットアッププロジェクトを選択して会社などを設定する場合は、必ず[プロパティ]タブをクリックしてください(右クリックではありません)。
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace FSWManager {
[RunInstaller(true)]
public partial class ProjectInstaller : Installer {
public ProjectInstaller() {
InitializeComponent();
this.FSWServiceMgr.AfterInstall += FSWServiceMgr_AfterInstall;
}
static void FSWServiceMgr_AfterInstall(object sender, InstallEventArgs e) {
new ServiceController("FSWServiceMgr").Start();
}
}
}