/// <summary> /// finds all the services visible from the current network location. /// </summary> /// <returns></returns> public static IEnumerable <ServiceLocator> FindServices() { // start net-view var local = new NetViewWrapper(); // wait for the network search to complete: local.Process.Wait(); // now enumerate the machines: foreach (var computerName in local.ComputerNames) { var services = default(IEnumerable <ServiceLocator>); try { services = GetServices(computerName); } catch (Exception accessException) { // skip this machine; Console.WriteLine($"Unable to Access Service-Manager on {computerName}: {accessException.Message}"); } if (services != null) { foreach (var svc in services) { yield return(svc); } } } }
/// <summary> /// enumerates all services on all machines. /// </summary> /// <returns></returns> public static IEnumerable <MapRecord> QueryWindowsNetworkServices() { // start the net wrapper: var network = new NetViewWrapper(); // wait for the process to find network computers: network.Process.Wait(); Console.WriteLine($"Found {network.ComputerNames.Count} computers in local network neighborhood"); // holder for output record List <MapRecord> maps = new List <MapRecord>(); // enumerate those machine-names found on the local network Parallel.ForEach(network.ComputerNames, (machine) => { // enumerate the services: Parallel.ForEach(ServiceLocator.GetServices(machine), (service) => { // push the machine and service name to the console for updates: Console.WriteLine($"{machine}.{service.ServiceName}"); try { // try to create the map record: maps.Add(MapRecord.Create(service)); } catch (Exception create) { // write some exception details to the console: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"Error Accessing Services on {machine}"); Console.WriteLine(create.Message); var trace = new System.Diagnostics.StackTrace(create); foreach (var sf in trace.GetFrames()) { Console.WriteLine($"{sf.GetMethod().Name} {System.IO.Path.GetFileName(sf.GetFileName())} {sf.GetFileLineNumber()}"); } Console.ForegroundColor = ConsoleColor.White; } }); }); return(maps); }