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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Zoran HorvatPosted Nov 16, 2010, 5:02 AM
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();
}
}