Esempio n. 1
0
		public List<VirtualSwitch> GetExternalSwitches(string computerName)
		{
			Wmi cwmi = new Wmi(computerName, WMI_VIRTUALIZATION_NAMESPACE);

			Dictionary<string, string> switches = new Dictionary<string, string>();
			List<VirtualSwitch> list = new List<VirtualSwitch>();

			// load external adapters
			Dictionary<string, string> adapters = new Dictionary<string, string>();
			ManagementObjectCollection objAdapters = cwmi.GetWmiObjects("Msvm_ExternalEthernetPort");
			foreach (ManagementObject objAdapter in objAdapters)
				adapters.Add((string)objAdapter["DeviceID"], "1");

			// get active connections
			ManagementObjectCollection objConnections = cwmi.GetWmiObjects("Msvm_ActiveConnection");
			foreach (ManagementObject objConnection in objConnections)
			{
				// check LAN andpoint
				ManagementObject objLanEndpoint = new ManagementObject(new ManagementPath((string)objConnection["Dependent"]));
				string endpointName = (string)objLanEndpoint["Name"];

				if (!endpointName.StartsWith("/DEVICE/"))
					continue;

				endpointName = endpointName.Substring(8);

				if (adapters.ContainsKey(endpointName))
				{
					// get switch port
					ManagementObject objPort = new ManagementObject(new ManagementPath((string)objConnection["Antecedent"]));
					string switchId = (string)objPort["SystemName"];
					if (switches.ContainsKey(switchId))
						continue;

					// add info about switch
					ManagementObject objSwitch = cwmi.GetRelatedWmiObject(objPort, "Msvm_VirtualSwitch");
					switches.Add(switchId, (string)objSwitch["ElementName"]);
				}
			}

			foreach (string switchId in switches.Keys)
			{
				VirtualSwitch sw = new VirtualSwitch();
				sw.SwitchId = switchId;
				sw.Name = switches[switchId];
				list.Add(sw);
			}

			return list;
		}