Search code examples
c#methodsnetwork-programmingwmi

When The RPC Server is unavailable How Can I Pass It?


Here is my codes. I am trying to get computer information on network and I have two methods to get them. But when the machine is not online I got this Error. How can I pass these machine when they are not online.

public string GetBoardSerNo(string machineName)
    {
        string y = "";

        try
        {
            ManagementObjectSearcher searcher = new
                ManagementObjectSearcher("\\\\" + machineName + "\\root\\CIMV2", "SELECT * FROM Win32_BaseBoard");

            foreach (ManagementObject wmi in searcher.Get())
            {

                return wmi.GetPropertyValue("SerialNumber").ToString();


            }
        }
        finally
        {
            y = "Serial Number: Unknown";

        }

        return y;

    }
    public string GetModel(string machineName)
    {
        string x = "";

        try
        {

            ManagementObjectSearcher searcher = new
                ManagementObjectSearcher("\\\\" + machineName + "\\root\\CIMV2", "SELECT * FROM Win32_ComputerSystem");

            foreach (ManagementObject wmi in searcher.Get())
            {
                return wmi.GetPropertyValue("Model").ToString();

            }

        }
        finally
        {
            x = "Model No: Unknown";

        }
        return x;

    }

Solution

  • I solved it by myself. Thanks for your help.

        {
    
            try
            {
                ManagementObjectSearcher searcher = new
                    ManagementObjectSearcher("\\\\" + machineName + "\\root\\CIMV2", "SELECT * FROM Win32_BaseBoard");
    
                foreach (ManagementObject wmi in searcher.Get())
                {
    
                    return wmi.GetPropertyValue("SerialNumber").ToString();
    
    
                }
            }
            catch (COMException ce)
            {
                if ((uint)ce.ErrorCode == 0x800706BA)
                {
                    return "Serial Number : Null";
                }
            }
    
            return "Serial Number : Null";
    
    
        }