/// <summary> /// Form load event, sets validation and loads device list from stored file. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FormConfigure_Load(object sender, EventArgs e) { string szSettingsFile = ConfigurationSettings.DeviceSettingsPath; // Setup validation and load devices for devices tab. this.txtName.Validating += ValidateForm; this.txtAddress.Validating += ValidateForm; this.txtPort.Validating += ValidateForm; this.txtCommunityRO.Validating += ValidateForm; this.txtUsername.Validating += ValidateForm; this.txtAuth.Validating += ValidateForm; this.txtPriv.Validating += ValidateForm; storedDevices = DeviceConfigurationList.GetDeviceList(szSettingsFile); RefreshDeviceList(); // Load settings tab data. if (szSettingsFile != ConfigurationSettings.DefaultStoragePath) { checkStorage.Checked = true; txtStorage.Enabled = true; txtStorage.Text = szSettingsFile; } if (ConfigurationSettings.DNSLookups) { checkDNS.Checked = true; } if (ConfigurationSettings.SNMPTimeout != 10000) { checkTimeout.Checked = true; txtTimeout.Value = ConfigurationSettings.SNMPTimeout / 1000; } }
/// <summary> /// Serializes a DeviceConfigurationList object to a file. /// </summary> /// <param name="filename"></param> /// <param name="objectToSerialize"></param> public void SerializeObject(string filename, DeviceConfigurationList objectToSerialize) { Stream stream = File.Open(filename, FileMode.Create); BinaryFormatter bFormatter = new BinaryFormatter(); bFormatter.Serialize(stream, objectToSerialize); stream.Close(); }
/// <summary> /// Deserializes the configuration and populates the combo box with the device list. /// </summary> private void RefreshDeviceList() { storedDevices.Devices.Clear(); combDevices.Items.Clear(); storedDevices = new DeviceConfigurationSerializer().DeSerializeObject(ConfigurationSettings.DeviceSettingsPath); foreach (DeviceConfiguration device in storedDevices.Devices) { combDevices.Items.Add(device.Name); } }
/// <summary> /// Serializes the list and saves to disk. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSave_Click(object sender, EventArgs e) { addDeviceToStorage(); try { DeviceConfigurationList.SaveDeviceList(ConfigurationSettings.DeviceSettingsPath, storedDevices); } catch (Exception ex) { MessageBox.Show("Unable to save device configuration information." + System.Environment.NewLine + System.Environment.NewLine + ex.Message, "An error has occured", MessageBoxButtons.OK, MessageBoxIcon.Error); } btnSave.Enabled = false; }
/// <summary> /// Remove the selected item from the list. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDelete_Click(object sender, EventArgs e) { // Remove the device from the storage instance on this form. storedDevices.Devices.Remove( storedDevices.Devices.Find(d => d.Name == listDevices.SelectedItem.ToString()) ); // Remove from the user-visible lis.t listDevices.Items.Remove(listDevices.SelectedItem); // Serialize the object to disk once more. DeviceConfigurationList.SaveDeviceList(ConfigurationSettings.DeviceSettingsPath, storedDevices); ClearFormContents(); grpGeneral.Enabled = false; grpCommunities.Enabled = false; grpSecPriv.Enabled = false; btnDelete.Enabled = false; }
/// <summary> /// Serializes the device list to a file. /// </summary> /// <param name="szFileName"></param> /// <param name="devices"></param> public static void SaveDeviceList(string szFileName, DeviceConfigurationList devices) { DeviceConfigurationSerializer serializer = new DeviceConfigurationSerializer(); serializer.SerializeObject(szFileName, devices); }