Alvi

📅 2026-03-25T02:17:18.146Z
👁️ 15 katselukertaa
🔓 Julkinen


using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using System.Management;
using Microsoft.Win32;

public class NetworkScanner
{
    public class PCInfo
    {
        public string Hostname { get; set; }
        public string IPAddress { get; set; }
        public string OSVersion { get; set; }
        public string DotNetVersion { get; set; }
    }

    public static async Task<List<PCInfo>> GetOnlineDomainPCsDetailedAsync()
    {
        var results = new ConcurrentBag<PCInfo>();
        var computers = GetAllDomainComputers();

        var tasks = new List<Task>();

        foreach (var pc in computers)
        {
            tasks.Add(Task.Run(async () =>
            {
                try
                {
                    using (Ping ping = new Ping())
                    {
                        var reply = await ping.SendPingAsync(pc, 800);

                        if (reply.Status == IPStatus.Success)
                        {
                            var info = new PCInfo
                            {
                                Hostname = pc,
                                IPAddress = reply.Address.ToString()
                            };

                            // Get OS + .NET via WMI + Registry
                            GetSystemInfo(pc, info);

                            results.Add(info);
                        }
                    }
                }
                catch
                {
                    // Ignore offline/unreachable
                }
            }));
        }

        await Task.WhenAll(tasks);
        return new List<PCInfo>(results);
    }

    private static void GetSystemInfo(string hostname, PCInfo info)
    {
        try
        {
            var scope = new ManagementScope($"\\\\{hostname}\\root\\cimv2");
            scope.Connect();

            // OS Version
            var query = new ObjectQuery("SELECT Caption, Version FROM Win32_OperatingSystem");
            using (var searcher = new ManagementObjectSearcher(scope, query))
            {
                foreach (ManagementObject os in searcher.Get())
                {
                    info.OSVersion = $"{os["Caption"]} ({os["Version"]})";
                }
            }

            // .NET Version (Registry)
            info.DotNetVersion = GetDotNetVersionRemote(hostname);
        }
        catch
        {
            info.OSVersion = "Access Denied / Unavailable";
            info.DotNetVersion = "Unavailable";
        }
    }

    private static string GetDotNetVersionRemote(string hostname)
    {
        try
        {
            using (var baseKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, hostname))
            using (var subKey = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full"))
            {
                if (subKey != null && subKey.GetValue("Release") != null)
                {
                    int release = (int)subKey.GetValue("Release");
                    return CheckDotNetVersion(release);
                }
            }
        }
        catch
        {
            return "Unavailable";
        }

        return "Not Installed";
    }

    private static string CheckDotNetVersion(int release)
    {
        if (release >= 533320) return ".NET 4.8.1";
        if (release >= 528040) return ".NET 4.8";
        if (release >= 461808) return ".NET 4.7.2";
        if (release >= 461308) return ".NET 4.7.1";
        if (release >= 460798) return ".NET 4.7";
        if (release >= 394802) return ".NET 4.6.2";
        if (release >= 394254) return ".NET 4.6.1";
        if (release >= 393295) return ".NET 4.6";
        if (release >= 379893) return ".NET 4.5.2";
        if (release >= 378675) return ".NET 4.5.1";
        if (release >= 378389) return ".NET 4.5";

        return "Unknown";
    }

    private static List<string> GetAllDomainComputers()
    {
        var list = new List<string>();

        using (DirectoryEntry entry = new DirectoryEntry("LDAP://RootDSE"))
        {
            string context = entry.Properties["defaultNamingContext"].Value.ToString();

            using (DirectoryEntry searchRoot = new DirectoryEntry($"LDAP://{context}"))
            using (DirectorySearcher searcher = new DirectorySearcher(searchRoot))
            {
                searcher.Filter = "(objectClass=computer)";
                searcher.PropertiesToLoad.Add("name");

                foreach (SearchResult result in searcher.FindAll())
                {
                    if (result.Properties.Contains("name"))
                        list.Add(result.Properties["name"][0].ToString());
                }
            }
        }

        return list;
    }
}