예제 #1
0
        public static bool SetSerialPort(this ManagementObject VM, string PipeName, int PortNumber = 1)
        {
            // Sanity Check
            if (VM == null || !VM["__CLASS"].ToString().Equals(VMStrings.ComputerSystem, StringComparison.InvariantCultureIgnoreCase))
            {
                return(false);
            }

            ManagementObject SP = VM.GetDevices().FirstOrDefault(D => D["ResourceSubType"].ToString()
                                                                 .Equals(ResourceSubTypes.SerialPort, StringComparison.InvariantCultureIgnoreCase) &&
                                                                 D["Caption"].ToString().EndsWith("" + PortNumber));

            if (SP == null)
            {
                return(false);
            }

            SP["Connection"] = new string[] { PipeName };
            return(VM.ModifyDevice(SP));
        }