private void loadXMLFile(string path) { XMLSettings.Settings s = (XMLSettings.Settings)XMLSettings.ReadXML(typeof(XMLSettings.Settings), path); if (s != null && s.KeysSounds != null && s.KeysSounds.Length > 0) { var items = new List <ListViewItem>(); string errors = ""; string sameKeys = ""; for (int i = 0; i < s.KeysSounds.Length; i++) { int kLength = s.KeysSounds[i].Keys.Length; bool keysNull = (kLength >= 1 && !s.KeysSounds[i].Keys.Any(x => x != 0)); int sLength = s.KeysSounds[i].SoundLocations.Length; bool soundsNotEmpty = s.KeysSounds[i].SoundLocations.All(x => !string.IsNullOrWhiteSpace(x)); Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath); bool filesExist = s.KeysSounds[i].SoundLocations.All(x => File.Exists(x)); if (keysNull || sLength < 1 || !soundsNotEmpty || !filesExist) //error in XML file { string tempErr = ""; if (!keysNull) { tempErr = "one or more keys are null"; } else if (sLength < 1) { tempErr = "no sounds provided"; } else if (!filesExist) { tempErr = "one or more sounds do not exist"; } errors += "Entry #" + i.ToString() + "has an error: " + tempErr + "\r\n"; } string keys = (kLength < 1 ? "" : Helper.keysToString(s.KeysSounds[i].Keys)); if (keys != "" && items.Count > 0 && items[items.Count - 1].Text == keys && !sameKeys.Contains(keys)) { sameKeys += (sameKeys != "" ? ", " : "") + keys; } var temp = new ListViewItem(keys); temp.SubItems.Add((sLength < 1 ? "" : Helper.soundLocsArrayToString(s.KeysSounds[i].SoundLocations))); items.Add(temp); //add even if there was an error, so that the user can fix within the app } if (items.Count > 0) { if (errors != "") { MessageBox.Show((errors == "" ? "" : errors)); } if (sameKeys != "") { MessageBox.Show("Multiple entries using the same keys. The keys being used multiple times are: " + sameKeys); } keysSounds.Clear(); keysSounds.AddRange(s.KeysSounds); lvKeySounds.Items.Clear(); lvKeySounds.Items.AddRange(items.ToArray()); chKeys.Width = -2; chSoundLoc.Width = -2; xmlLoc = path; } else { SystemSounds.Beep.Play(); MessageBox.Show("No entries found, or all entries had errors in them (key being None, sound location behind empty or non-existant)"); } } else { SystemSounds.Beep.Play(); MessageBox.Show("No entries found, or there was an error reading the settings file"); } }
private bool loadXMLFile(string path) { bool errorOccurred = true; try { XMLSettings.Settings s = (XMLSettings.Settings)XMLSettings.ReadXML(typeof(XMLSettings.Settings), path); if (s != null && s.SoundHotkeys != null && s.SoundHotkeys.Length > 0) { var items = new List <ListViewItem>(); string errorMessage = ""; string sameKeys = ""; for (int i = 0; i < s.SoundHotkeys.Length; i++) { int kLength = s.SoundHotkeys[i].Keys.Length; bool keysNull = (kLength > 0 && !s.SoundHotkeys[i].Keys.Any(x => x != 0)); int sLength = s.SoundHotkeys[i].SoundLocations.Length; bool soundsNotEmpty = s.SoundHotkeys[i].SoundLocations.All(x => !string.IsNullOrWhiteSpace(x)); //false if even one location is empty Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath); bool filesExist = s.SoundHotkeys[i].SoundLocations.All(x => File.Exists(x)); if (keysNull || sLength < 1 || !soundsNotEmpty || !filesExist) //error in XML file { string tempErr = ""; if (kLength == 0 && (sLength == 0 || !soundsNotEmpty)) { tempErr = "entry is empty"; } else if (!keysNull) { tempErr = "one or more keys are null"; } else if (sLength == 0) { tempErr = "no sounds provided"; } else if (!filesExist) { tempErr = "one or more sounds do not exist"; } errorMessage += "Entry #" + (i + 1).ToString() + " has an error: " + tempErr + "\r\n"; } string keys = (kLength < 1 ? "" : Helper.keysArrayToString(s.SoundHotkeys[i].Keys)); if (keys != "" && items.Count > 0 && items[items.Count - 1].Text == keys && !sameKeys.Contains(keys)) { sameKeys += (sameKeys != "" ? ", " : "") + keys; } string windowString = string.IsNullOrWhiteSpace(s.SoundHotkeys[i].WindowTitle) ? "" : s.SoundHotkeys[i].WindowTitle; string volumeString = s.SoundHotkeys[i].SoundVolume == 1 ? "" : Helper.linearVolumeToString(s.SoundHotkeys[i].SoundVolume); string soundLocations = sLength < 1 ? "" : Helper.fileLocationsArrayToString(s.SoundHotkeys[i].SoundLocations); var temp = new ListViewItem(keys); temp.SubItems.Add(volumeString); temp.SubItems.Add(windowString); temp.SubItems.Add(soundLocations); temp.ToolTipText = Helper.getFileNamesTooltip(s.SoundHotkeys[i].SoundLocations); //blank tooltips are not displayed items.Add(temp); //add even if there was an error, so that the user can fix within the app } if (items.Count > 0) { if (errorMessage != "") { MessageBox.Show((errorMessage == "" ? "" : errorMessage)); } else { errorOccurred = false; } if (sameKeys != "") { MessageBox.Show("Multiple entries using the same keys. The keys being used multiple times are: " + sameKeys); } soundHotkeys.Clear(); soundHotkeys.AddRange(s.SoundHotkeys); lvKeySounds.Items.Clear(); lvKeySounds.Items.AddRange(items.ToArray()); sortHotkeys(); xmlLocation = path; } else { SystemSounds.Beep.Play(); MessageBox.Show("No entries found, or all entries had errors in them (key being None, sound location behind empty or non-existant)"); } } else { SystemSounds.Beep.Play(); MessageBox.Show("No entries found, or there was an error reading the settings file"); } } catch { MessageBox.Show("Settings file structure is incorrect"); } return(errorOccurred); }