// // Copyright 2014 Perforce Software Inc. // using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Perforce.Helper { public class ExtensionHelper { private string _mappingFileName = Application.LocalUserAppDataPath + @"\mappings.json"; private Dictionary _mappings; public ExtensionHelper() { LoadMappings(); } public Dictionary Mappings { get { return _mappings; } set { _mappings = value; } } public string GetMapping(string extension) { string value = null; _mappings.TryGetValue(extension, out value); return value; } public void SetMapping(string extension, string value) { if (_mappings == null) { _mappings = new Dictionary(StringComparer.CurrentCultureIgnoreCase); } _mappings.Add(extension, value); } public void SaveMappings() { // convert mappings to json string json = JsonConvert.SerializeObject(_mappings, Formatting.Indented); // write to file File.WriteAllText(_mappingFileName, json); } public void LoadMappings() { if (_mappingFileName != null && File.Exists(_mappingFileName)) { // read json from file var json = File.ReadAllText(_mappingFileName); // convert to dictionary _mappings = JsonConvert.DeserializeObject>(json); } else { _mappings = new Dictionary(StringComparer.CurrentCultureIgnoreCase); SaveMappings(); } } } }