Example #1
0
        /// <summary>
        /// Save the Hotkeys to file.
        /// </summary>
        /// <returns>True or false on success</returns>
        public static bool SaveHotkeys()
        {
            bool ret = false;

            try
            {
                string result = JsonConvert.SerializeObject(hotkeys, new JsonSerializerSettings()
                {
                    NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.Indented
                });
                log.Debug($@"Saving hotkeys : {hotkeys}");
                string filepath = Path.Combine(path, "WinHue\\WinHueHotkeys.set");
                if (File.Exists(filepath))
                {
                    log.Info("Backuping WinHueHotkeys.set file to WinHueHotkeys.set.bak");
                    File.Copy(filepath, filepath + ".bak", true);
                }

                if (CreateWinHueDirectory())
                {
                    File.WriteAllText(filepath, result);
                    ret = true;
                }
            }
            catch (Exception ex)
            {
                hotkeys = new CustomHotkeys();
                ret     = false;
                log.Error("Error while saving the hotkeys.", ex);
            }
            return(ret);
        }
Example #2
0
        /// <summary>
        /// Load hotkeys from file
        /// </summary>
        /// <returns>True or false on success</returns>
        public static bool LoadHotkeys()
        {
            bool result = false;

            try
            {
                string filepath = Path.Combine(path, "WinHue\\WinHueHotkeys.set");
                if (!File.Exists(filepath))
                {
                    return(result);
                }
                log.Debug("Trying to open hotkeys file...");
                StreamReader sr = File.OpenText(filepath);
                log.Debug("File open.");
                string settingsString = sr.ReadToEnd();
                log.Debug($@"Loading hotkeys : {settingsString}");
                sr.Close();
                log.Debug("Deserializing the settings file.");
                hotkeys = JsonConvert.DeserializeObject <CustomHotkeys>(settingsString, new JsonSerializerSettings()
                {
                    NullValueHandling = NullValueHandling.Ignore
                });
                result = true;
            }
            catch (Exception ex)
            {
                settings = new CustomSettings();
                result   = false;
                log.Error("Error while loading the hotkeys.", ex);
            }
            return(result);
        }