双击启动Windows服务

发布于 2024-09-08 02:24:19 字数 188 浏览 1 评论 0原文

如何让我的 Windows 服务按以下方式工作...

1.) 安装后自动启动

2.) 即使我们简单地双击可执行文件也会自动启动

换句话说,我不想使用“NET” START”、“SC”命令,并且不想通过服务控制台启动它。我只想让我的服务自动安装并自动启动...加上双击可执行文件时自动启动。

谢谢。

How do I make my Windows Service to work in the following way...

1.) Automatically start after it installs

2.) Automatically start even if we simply double click on the executable

In other words,I dont want to use the "NET START","SC" commands and dont want to start it through the services console. I just want my Service to auto-install and auto start itself...plus start itself automatically when the executable is double clicked.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

家住魔仙堡 2024-09-15 02:24:19

查看 ServiceController 类。

您可以在 commited< 中使用它/code>事件如下:

[RunInstaller(true)]
public class ServiceInstaller : Installer
{
    string serviceName = "MyServiceName";

    public ServiceInstaller()
    {
        var processInstaller = new ServiceProcessInstaller();
        var serviceInstaller = new ServiceInstaller();

        processInstaller.Account = ...;
        processInstaller.Username = ...;
        processInstaller.Password = ...;

        serviceInstaller.DisplayName = serviceName;
        serviceInstaller.StartType = ServiceStartMode.Automatic;

        serviceInstaller.ServiceName = serviceName;

        this.Installers.Add(processInstaller);
        this.Installers.Add(serviceInstaller);

        this.Committed += new InstallEventHandler(ServiceInstaller_Committed);
    }

    void ServiceInstaller_Committed(object sender, InstallEventArgs e)
    {
        // Auto Start the Service Once Installation is Finished.
        var controller = new ServiceController(serviceName);
        controller.Start();
    }
}

Have a look at ServiceController class.

You can use it in commited event like this :

[RunInstaller(true)]
public class ServiceInstaller : Installer
{
    string serviceName = "MyServiceName";

    public ServiceInstaller()
    {
        var processInstaller = new ServiceProcessInstaller();
        var serviceInstaller = new ServiceInstaller();

        processInstaller.Account = ...;
        processInstaller.Username = ...;
        processInstaller.Password = ...;

        serviceInstaller.DisplayName = serviceName;
        serviceInstaller.StartType = ServiceStartMode.Automatic;

        serviceInstaller.ServiceName = serviceName;

        this.Installers.Add(processInstaller);
        this.Installers.Add(serviceInstaller);

        this.Committed += new InstallEventHandler(ServiceInstaller_Committed);
    }

    void ServiceInstaller_Committed(object sender, InstallEventArgs e)
    {
        // Auto Start the Service Once Installation is Finished.
        var controller = new ServiceController(serviceName);
        controller.Start();
    }
}
围归者 2024-09-15 02:24:19

看一下 Topshelf 项目 (http://topshelf-project.com) 并消除所有复杂性在.NET 中编写Windows 服务。它处理所有自注册并消除应用程序对服务代码的所有依赖。

它也是开源的并托管在 GitHub 上,因此可以轻松适应任何应用程序。

(完全披露,我是该项目的作者之一)

Take a look at the Topshelf project (http://topshelf-project.com) and eliminate all the complexity of writing Windows services in .NET. It handles all the self-registration and eliminates all the dependencies on service code from your application.

It's also open-source and hosted on GitHub, making it easy to adapt to any application.

(full disclosure, I am one of the authors on the project)

紫轩蝶泪 2024-09-15 02:24:19

您可以添加调用安装程序的命令行参数(使用 ManagedInstallerClass.InstallHelper() )以及启动服务的代码...

 public class DataImportService : ServiceBase
 {
     // ----------- Other code -----------

     static void Main(string[] args)
     {
       if (args.Length == 0) 
       {
            InstallService(false, argValue); break;
            StartService();
       }
       else
       {
            string arg0 = args[0],
            switchVal = arg0.ToUpper(),
            argValue = arg0.Contains(":") ?
            arg0.Substring(arg0.IndexOf(":")) : null;

            switch (switchVal.Substring(0, 1))
            {
                //Install Service and run
                case ("I"): case ("-I"): case ("/I"):
                    InstallService(true, argValue); break;

                 // Start Service
                case ("S"): case ("-S"): case ("/S"):
                    StartService();
                default: break;

                 // Install & Start Service
                case ("IS"): case ("-IS"): case ("/IS"):
                    InstallService(false, argValue); break;
                    StartService();

                // Uninstall Service
                case ("U"): case ("-U"): case ("/U"):
                    InstallService(false, argValue); break;

                default: break;                   
            }
        }

     private static void InstallService(bool install,  string argFileSpec)
     {
        string fileSpec = Assembly.GetExecutingAssembly().Location;
        if (!String.IsNullOrEmpty(argFileSpec)) fileSpec = argFileSpec;
        // ------------------------------------------------------------
        string[] installerParams =
            install? new string[] { fileSpec } :
                     new string[] { "/u", fileSpec };
        ManagedInstallerClass.InstallHelper(installerParams);
     }

     private void StartService()
     {
        var ctlr  = new ServiceController();
        ctlr.ServiceName = "MyService";    // hard code the service name
        // Start the service
        ctlr.Start();           
     }
}

You can add command line arguments that call the installer ( use ManagedInstallerClass.InstallHelper()), and code to start the service...

 public class DataImportService : ServiceBase
 {
     // ----------- Other code -----------

     static void Main(string[] args)
     {
       if (args.Length == 0) 
       {
            InstallService(false, argValue); break;
            StartService();
       }
       else
       {
            string arg0 = args[0],
            switchVal = arg0.ToUpper(),
            argValue = arg0.Contains(":") ?
            arg0.Substring(arg0.IndexOf(":")) : null;

            switch (switchVal.Substring(0, 1))
            {
                //Install Service and run
                case ("I"): case ("-I"): case ("/I"):
                    InstallService(true, argValue); break;

                 // Start Service
                case ("S"): case ("-S"): case ("/S"):
                    StartService();
                default: break;

                 // Install & Start Service
                case ("IS"): case ("-IS"): case ("/IS"):
                    InstallService(false, argValue); break;
                    StartService();

                // Uninstall Service
                case ("U"): case ("-U"): case ("/U"):
                    InstallService(false, argValue); break;

                default: break;                   
            }
        }

     private static void InstallService(bool install,  string argFileSpec)
     {
        string fileSpec = Assembly.GetExecutingAssembly().Location;
        if (!String.IsNullOrEmpty(argFileSpec)) fileSpec = argFileSpec;
        // ------------------------------------------------------------
        string[] installerParams =
            install? new string[] { fileSpec } :
                     new string[] { "/u", fileSpec };
        ManagedInstallerClass.InstallHelper(installerParams);
     }

     private void StartService()
     {
        var ctlr  = new ServiceController();
        ctlr.ServiceName = "MyService";    // hard code the service name
        // Start the service
        ctlr.Start();           
     }
}
深者入戏 2024-09-15 02:24:19

My post here shows how to have your Windows service install itself from the command line using a -install option. You could extend this logic to have a -start option and then create a shortcut on the desktop that includes that option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文