If you click the properties of a window service, you will see the dependency tab.
I need to find this out through my code and display the dependencies on my screen.


Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Zoran HorvatRank #792 · 2.9K reputationPosted Nov 16, 2010, 12:02 AM EST
This code prints all services with all their depending-on services:
static void Main(string[] args)
{
System.ServiceProcess.ServiceController[] services = System.ServiceProcess.ServiceController.GetServices();
int nameLen = 0;
for (int i = 0; i < services.Length; i++)
nameLen = Math.Max(nameLen, services[i].ServiceName.Length);
string fmt = "{0," + nameLen.ToString() + "} ->";
for (int i = 0; i < services.Length; i++)
{
Console.Write(fmt, services[i].ServiceName);
System.ServiceProcess.ServiceController[] parents = services[i].ServicesDependedOn;
for (int j = 0; j < parents.Length; j++)
{
Console.Write("{0}", j == 0 ? " " : ", ");
try
{
Console.Write("{0}", parents[j].ServiceName);
}
catch
{
Console.Write("
}
}
Console.WriteLine();
}
}