/* * Given an file with the following form: * [section1] * key1=value * key2=value * key3=value * [section2] * key1=value * key2=value * * Then parse the file into a Dictionary<string,string> and return it. * * Users: dict["section"]["key2"] is "value" */ public static async Task <Dictionary <string, Dictionary <string, string> > > LoadSettingsFile(StorageFolder folder, string filename) { KeyValuePair <string, string> currentKvp = new KeyValuePair <string, string>(); Dictionary <string, Dictionary <string, string> > returnDictionary = new Dictionary <string, Dictionary <string, string> >(); var file = await folder.GetFileAsync(filename); string contents = await FileIO.ReadTextAsync(file); contents = contents.Replace('\r', '\n'); Dictionary <string, string> sectionsDict = null; try { sectionsDict = StaticHelpers.GetSections(contents); } catch (Exception e) { string content = String.Format($"Error parsing file {filename}.\nIn File: {folder.Path}\n\nSuggest deleting it.\n\nError parsing sections.\nException info: {e.ToString()}"); MessageDialog dlg = new MessageDialog(content); await dlg.ShowAsync(); return(returnDictionary); } if (sectionsDict.Count == 0) { string content = String.Format($"There appears to be no sections in {filename}.\nIn File: {folder.Path}\n\nSuggest deleting it.\n\nError parsing sections."); MessageDialog dlg = new MessageDialog(content); await dlg.ShowAsync(); return(returnDictionary); } try { foreach (var kvp in sectionsDict) { currentKvp = kvp; var dict = DeserializeDictionary(kvp.Value); returnDictionary[kvp.Key] = dict; } } catch { string content = String.Format($"Error parsing values {folder.Path}\\{filename}.\nSuggest deleting it.\n\nError in section '{currentKvp.Key}' and value '{currentKvp.Value}'"); MessageDialog dlg = new MessageDialog(content); await dlg.ShowAsync(); } return(returnDictionary); }
public static Dictionary <string, string> GetSection(string file, string section) { Dictionary <string, string> sections = StaticHelpers.GetSections(file); if (sections == null) { return(null); } return(StaticHelpers.DeserializeDictionary(sections[section])); }
public static async Task <Dictionary <string, string> > LoadSectionsFromFile(StorageFolder folder, string filename) { var file = await folder.GetFileAsync(filename); string contents = await FileIO.ReadTextAsync(file); contents = contents.Replace('\r', '\n'); Dictionary <string, string> sectionsDict = null; try { sectionsDict = StaticHelpers.GetSections(contents); } catch (Exception e) { string content = String.Format($"Error parsing file {filename}.\nIn File: {folder.Path}\n\nSuggest deleting it.\n\nError parsing sections.\nException info: {e.ToString()}"); MessageDialog dlg = new MessageDialog(content); await dlg.ShowAsync(); } return(sectionsDict); }