你可以把設置在.exe.config到每個服務 ,並觀察他們這樣
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;
}
}
你嘗試獲取並覈對姓名?你能顯示一些代碼嗎? –
難道你只是想檢查'ServiceName'屬性嗎? –
我將使用服務名稱屬性,但又是硬編碼的東西。 – Meraj