确定服务是否已安装

发布于 2024-12-04 13:25:20 字数 857 浏览 3 评论 0原文

作为安装 Windows 服务的一部分,我编写了 installutil 和命令行的替代方案:

IDictionary state = new Hashtable();
using (AssemblyInstaller inst = new AssemblyInstaller(typeof(MyServiceClass).Assembly, args))
{
    IDictionary state = new Hashtable();
    inst.UseNewContext = true;
    //# Service Account Information
    inst.Install(state);
    inst.Commit(state);
}

安装它。我通过检测是否启动成功来确定是否需要执行此操作。我预计请求启动和实际设置 RunningOk 标志之间会有延迟,并且宁愿采用通用的编程解决方案。 ( 如何以编程方式安装 Windows 服务C#? ) http://dl.dropbox.com/u/152585/ServiceInstaller.cs 解决方案已经有近 3 年历史了,很长,并且导入了 DLL,从我对 .NET 的了解来看,这似乎破坏了它的安全性意图。

因此,我想知道一种更简洁的方法来使用 .NET 执行此操作(如果存在)?

As part of installing my windows service I have coded an alternative to installutil and the command line:

IDictionary state = new Hashtable();
using (AssemblyInstaller inst = new AssemblyInstaller(typeof(MyServiceClass).Assembly, args))
{
    IDictionary state = new Hashtable();
    inst.UseNewContext = true;
    //# Service Account Information
    inst.Install(state);
    inst.Commit(state);
}

to intall it. I determing whether this needs doing by detecting whether it starts successfully. I anticipate delay between requesting it start and it actually setting it's RunningOk flag and would rather a generic, programmatic solution. The ( How to install a windows service programmatically in C#? ) http://dl.dropbox.com/u/152585/ServiceInstaller.cs solution is nearly 3 years old, long, and imports DLL's which from the little I know about .NET seems to defeat its security intentions.

Hence I would like to know of a more concise way to do this with .NET, if one exists?

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

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

发布评论

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

评论(2

厌倦 2024-12-11 13:25:20

要查找服务以及它是否正在运行,

    private static bool IsServiceInstalled(string serviceName)
    {
        ServiceController[] services = ServiceController.GetServices();
        foreach (ServiceController service in services)
            if (service.ServiceName == serviceName) return true;
        return false;
    }

    private static bool IsServiceInstalledAndRunning(string serviceName)
    {
        ServiceController[] services = ServiceController.GetServices();
        foreach (ServiceController service in services)
            if (service.ServiceName == serviceName) return service.Status != ServiceControllerStatus.Stopped;
        return false;
    }

请注意,它实际上检查它是否未停止,而不是它是否正在运行,但如果您愿意,您可以更改它。

很高兴在简洁的 .NET 结构中做到这一点。不记得我从哪里得到这些信息,但现在看起来很简单,值得发帖。

To find a service, and whether it is running,

    private static bool IsServiceInstalled(string serviceName)
    {
        ServiceController[] services = ServiceController.GetServices();
        foreach (ServiceController service in services)
            if (service.ServiceName == serviceName) return true;
        return false;
    }

    private static bool IsServiceInstalledAndRunning(string serviceName)
    {
        ServiceController[] services = ServiceController.GetServices();
        foreach (ServiceController service in services)
            if (service.ServiceName == serviceName) return service.Status != ServiceControllerStatus.Stopped;
        return false;
    }

Note it actually checks whether it is not stopped instead of whether it is running, but you can change that if you wish.

Nice to do this in concise .NET constructs. Don't recall where I got the info from but it looks so easy now and worth a post.

浪菊怪哟 2024-12-11 13:25:20

我建议使用Windows Installer 本机支持< /a>.

您可以管理服务安装、卸载以及启动或停止操作。

不幸的是,并非所有设置创作工具都提供对此的直接支持,因此您需要找到一个能够提供直接支持的工具。

I recommend using the Windows Installer native support.

You can manage the service install, uninstall and start or stop operations.

Unfortunately not all setup authoring tools offer direct support for this, so you will need to find one that does.

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