开发者

windows service development c#

I'm devloping one application which manages our custom made windows service. I'm using ServiceController object to get all services, but after that how I will differentiate which is our custom service and which is system service?

I am using following code:

ListViewItem datalist;                  

services = ServiceController.GetServices();                                     
ServiceList.Items.Clear();  
foreach(ServiceController service in services)
{                 开发者_如何学运维                      
    datalist = new System.Windows.Forms.ListViewItem(service.ServiceName.ToString());   

    datalist.SubItems.Add(service.DisplayName);    
    datalist.SubItems.Add(service.Status.ToString());                   
    ServiceList.Items.Add(datalist);                                    
}           


Put the name of your services in app.config. And read those when your application is started.

You can use http://csd.codeplex.com/ to create a custom configuration section.


You can put settings in .exe.config to each of your services and observe them like this

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Management;

class Program
{
    static void Main(string[] args)
    {
        var searcher =
            new ManagementObjectSearcher("SELECT * FROM Win32_Service WHERE Started=1 AND StartMode=\"Auto\"");
        foreach (ManagementObject service in searcher.Get())
        {
            foreach (var prop in service.Properties)
            {
                if (prop.Name != "PathName" || prop.Value == null)
                    continue;
                var cmdLine = prop.Value.ToString();
                var path = cmdLine.SplitCommandLine().ToArray()[0] + ".config";
                if (File.Exists(path))
                {
                    var serviceConfig = ConfigurationManager.OpenExeConfiguration(path);
/***/
                }
                break;
            }
        }
    }
}

SplitCommand

static class SplitCommand
{
    public static IEnumerable<string> Split(this string str, Func<char, bool> controller)
    {
        int nextPiece = 0; for (int c = 0; c < str.Length; c++)
        {
            if (controller(str[c]))
            { yield return str.Substring(nextPiece, c - nextPiece); nextPiece = c + 1; }
        } yield return str.Substring(nextPiece);
    }
    public static IEnumerable<string> SplitCommandLine(this string commandLine)
    {
        bool inQuotes = false;
        return commandLine.Split(c =>
        {
            if (c == '\"')
                inQuotes = !inQuotes; return !inQuotes && c == ' ';
        }).Select(arg => arg.Trim().TrimMatchingQuotes('\"')).Where(arg => !string.IsNullOrEmpty(arg));
    }
    public static string TrimMatchingQuotes(this string input, char quote)
    {
        if ((input.Length >= 2) && (input[0] == quote) && (input[input.Length - 1] == quote))
            return input.Substring(1, input.Length - 2);
        return input;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜