/// <summary>
        /// Obtain list of monitored decvices that are to be included in list of network adapters that can be enabled/disabled
        /// </summary>
        /// <returns>Array of Adapter Names to be monitored</returns>
        private static MonitoredDevice[] ReadMonitoredDevicesFromConfig()
        {
            const string MemberName = "ReadMonitoredDevicesFromConfig";

            using (new Tracer(MemberName))
            {
                List <MonitoredDevice> monitoredDevices = new List <MonitoredDevice>();

                try
                {
                    MonitoredDevicesSection mds = (MonitoredDevicesSection)ConfigurationManager.GetSection("MonitoredDevices");

                    for (int i = 0; i < mds.Items.Count; i++)
                    {
                        MonitoredDevice monitoredDevice = new MonitoredDevice(mds.Items[i].Device, mds.Items[i].PnPDevice, mds.Items[i].DeviceType);
                        monitoredDevices.Add(monitoredDevice);
                    }
                }
                catch (Exception ex)
                {
                    //Do not throw an exception just log it
                    ConnectionMonitorException cme = new ConnectionMonitorException(
                        "No Monitored Devices defined in the configuration file! " + MemberName, ex);
                    Logger.Write(cme);
                }

                return(monitoredDevices.ToArray());
            }
        }
Beispiel #2
0
        /// <summary>
        /// Retrieves the list of monitored devices from the Connection Monitor Configuration file.
        /// </summary>
        /// <param name="deviceToReplace">OPTIONAL - If an instance of Device is passed, will replace what is read from the configuration with the instance data passed and returns the list. NOTE: this method does not save the configuration file so passing this instance does not write it out to the configuration, this is only reflected in memory, not persisted!</param>
        /// <returns>List of Device instances build from the configuration file</returns>
        private List <Device> getMonitoredDeviceList(Device deviceToReplace = null)
        {
            List <Device>           devices             = new List <Device>();
            MonitoredDevicesSection mds                 = MonitoredDevicesSection.LoadConfiguration(this.conMonServiceConfigFileLocation);
            List <string>           deviceTypeOrderList = this.getDeviceTypeOrderList();

            foreach (string deviceTypeOrder in deviceTypeOrderList)
            {
                foreach (MonitoredDeviceElement monitoredDeviceConfigElement in mds.Items)
                {
                    if (string.Compare(monitoredDeviceConfigElement.DeviceType, deviceTypeOrder) == 0 ||
                        (deviceToReplace != null && string.Compare(deviceToReplace.DeviceType, deviceTypeOrder) == 0))
                    {
                        Device deviceItem = null;

                        if (deviceToReplace != null && deviceToReplace.DeviceName == monitoredDeviceConfigElement.Device)
                        {
                            deviceItem = deviceToReplace;
                        }
                        else
                        {
                            deviceItem = new Device(monitoredDeviceConfigElement);
                        }
                        deviceItem.MouseDoubleClick += new MouseButtonEventHandler(monitoredDeviceItem_MouseDoubleClick);
                        devices.Add(deviceItem);
                    }
                }
            }

            return(devices);
        }
Beispiel #3
0
        private bool updateMonitoredDevicesConfigurationSection(string configFilename)
        {
            bool success = false;

            try
            {
                TryEnableWireless();

                List <string> wirelessAdapters = GetWirelessAdapterList();
                if (wirelessAdapters == null)
                {
                    throw new InstallException("Connection Monitor could not find any wireless adapters to manage.");
                }

                bool saveMonitoredDevicesSection = false;

                MonitoredDevicesSection mds = MonitoredDevicesSection.LoadConfiguration(configFilename);
                if (mds == null)
                {
                    throw new InstallException("MonitoredDevices section not found.");
                }

                List <string> monitoredDevices = mds.Items.GetAllMonitoredDeviceNames();
                if (monitoredDevices == null)
                {
                    throw new InstallException("Error occured getting all monitored devices.");
                }

                foreach (string wirelessAdapter in wirelessAdapters)
                {
                    if (!monitoredDevices.Contains(wirelessAdapter))
                    {
                        MonitoredDeviceElement wirelessDeviceElement = new MonitoredDeviceElement();
                        wirelessDeviceElement.Device     = wirelessAdapter;
                        wirelessDeviceElement.PnPDevice  = wirelessAdapter;
                        wirelessDeviceElement.DeviceType = "Wireless";

                        mds.Items.AddAt(mds.Items.StartingIndexForAddingWirelessMonitoredDevices(), wirelessDeviceElement);
                        saveMonitoredDevicesSection = true;
                    }
                }

                if (saveMonitoredDevicesSection)
                {
                    if (!MonitoredDevicesSection.SaveConfiguration(configFilename, mds))
                    {
                        throw new InstallException("Failed to save configuration file.");
                    }
                }

                success = true;
            }
            catch (Exception ex)
            {
                throw new InstallException("Failed to update MonitoredDevices section of the configuration file.", ex);
            }

            return(success);
        }
Beispiel #4
0
        /// <summary>
        /// Retrieve the device type ordering list from the configuration file.
        /// </summary>
        /// <returns>List of device types in order as they appear in the configuration file</returns>
        private List <string> getDeviceOrderListFromConfig()
        {
            List <string>           deviceTypes = new List <string>();
            MonitoredDevicesSection mds         = MonitoredDevicesSection.LoadConfiguration(this.conMonServiceConfigFileLocation);

            foreach (MonitoredDeviceElement monitoredDeviceConfigElement in mds.Items)
            {
                if (!deviceTypes.Contains(monitoredDeviceConfigElement.DeviceType))
                {
                    deviceTypes.Add(monitoredDeviceConfigElement.DeviceType);
                }
            }

            return(deviceTypes);
        }
Beispiel #5
0
        /// <summary>
        /// Saves changes made in the application by the user back to the Connection Monitor configuration file.
        /// </summary>
        private bool saveChanges()
        {
            bool success = false;
            MonitoredDevicesSection mds = new MonitoredDevicesSection();

            foreach (Device monitoredDevice in this.MonitoredDevicesListBox.Items)
            {
                MonitoredDeviceElement monitoredDeviceElement = new MonitoredDeviceElement();
                monitoredDeviceElement.Device     = monitoredDevice.DeviceName;
                monitoredDeviceElement.DeviceType = monitoredDevice.DeviceType;
                monitoredDeviceElement.PnPDevice  = monitoredDevice.PnPDeviceName;
                mds.Items.Add(monitoredDeviceElement);
            }

            success = MonitoredDevicesSection.SaveConfiguration(
                this.conMonServiceConfigFileLocation, mds);
            return(success);
        }