52

Visual Studio セットアップ プロジェクトからインストールを実行した後、サービスを自動的に開始するにはどうすればよいですか?

私はこれを理解し、一般的な利益のために答えを共有すると思いました. 従うべき答え。私はこれを行うための他のより良い方法を受け入れています。

4

7 に答える 7

56

次のクラスをプロジェクトに追加します。

using System.ServiceProcess;  

class ServInstaller : ServiceInstaller
{
    protected override void OnCommitted(System.Collections.IDictionary savedState)
    {
        ServiceController sc = new ServiceController("YourServiceNameGoesHere");
        sc.Start();
    }
}

セットアップ プロジェクトはクラスを取得し、インストーラーの終了後にサービスを実行します。

于 2008-10-17T15:52:35.703 に答える
25

このアプローチでは、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プロパティも設定します。

于 2012-05-26T01:02:33.307 に答える
10

実行してくれてありがとう...

private System.ServiceProcess.ServiceInstaller serviceInstaller1;

private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
    ServiceController sc = new ServiceController("YourServiceName");
    sc.Start();
}
于 2009-04-09T04:06:08.047 に答える
7

独自のクラスを作成する代わりに、プロジェクト インストーラーでサービス インストーラーを選択し、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();
    }
}

スタートアップの種類が自動に設定されている場合にのみ、サービスが開始されます。

于 2012-05-10T09:37:43.577 に答える
3

上記のスニペットに基づくと、私の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();
        }
    }
}
于 2012-05-19T00:17:05.917 に答える