diff --git a/Desktop Notes/Desktop Notes/MainForm.cs b/Desktop Notes/Desktop Notes/MainForm.cs index 08c0ccc..3d191a8 100644 --- a/Desktop Notes/Desktop Notes/MainForm.cs +++ b/Desktop Notes/Desktop Notes/MainForm.cs @@ -39,73 +39,94 @@ public MainForm(int id, FormData dat = null) private void LoadData(FormData dat = null) { - //load defaults - this.StartPosition = FormStartPosition.WindowsDefaultLocation; - this.CreationTime = DateTime.Now; - if (string.IsNullOrEmpty(Title)) - { - Title = string.Format("Note {0:##}", FORM_ID); - } - - // 如果是新建便签,应用上次保存的默认设置 - if (dat == null) + _isLoading = true; + try { - DefaultNoteSettings defaultSettings = REGISTRY.GetDefaultSettings(); - this.Opacity = defaultSettings.opacity; - - if (defaultSettings.customTheme != null) + //load defaults + this.StartPosition = FormStartPosition.WindowsDefaultLocation; + this.CreationTime = DateTime.Now; + if (string.IsNullOrEmpty(titlebar.Text)) { - this.CustomTheme = defaultSettings.customTheme; - } - if (defaultSettings.customStyle != null) - { - this.CustomStyle = defaultSettings.customStyle; + titlebar.Text = string.Format("Note {0:##}", FORM_ID); } - if (Program.Themes.Count > defaultSettings.theme) + // 如果是新建便签,应用上次保存的默认设置 + if (dat == null) { - CurrentTheme = defaultSettings.theme; - } - else if (Program.Themes.Count > 1) - { - CurrentTheme = 1; + DefaultNoteSettings defaultSettings = REGISTRY.GetDefaultSettings(); + this.Opacity = defaultSettings.opacity; + + if (defaultSettings.customTheme != null) + { + this.CustomTheme = defaultSettings.customTheme; + } + if (defaultSettings.customStyle != null) + { + this.CustomStyle = defaultSettings.customStyle; + } + + if (Program.Themes.Count > defaultSettings.theme) + { + _theme = defaultSettings.theme; + } + else if (Program.Themes.Count > 1) + { + _theme = 1; + } + ReloadTheme(); + + if (Program.Styles.Count > defaultSettings.style) + { + _style = defaultSettings.style; + } + else if (Program.Styles.Count > 1) + { + _style = 1; + } + ReloadStyle(); + + _isLoading = false; + Save(); + this.Show(); + return; } - if (Program.Styles.Count > defaultSettings.style) - { - CurrentStyle = defaultSettings.style; - } - else if (Program.Styles.Count > 1) - { - CurrentStyle = 1; - } + //load others + this.SuspendLayout(); + this.StartPosition = FormStartPosition.Manual; + this.Location = dat.Location; + this.Size = dat.FormSize; + this.notebox1.Rtf = dat.data; + this.Opacity = dat.opacity; + titlebar.Text = dat.title; + if (dat.customTheme != null) { this.CustomTheme = dat.customTheme; } + _theme = dat.theme; + if (_theme >= Program.Themes.Count) _theme = Program.Themes.Count - 1; + if (_theme < 0) _theme = 0; + ReloadTheme(); + if (dat.customStyle != null) { this.CustomStyle = dat.customStyle; } + _style = dat.currentStyle; + if (_style >= Program.Styles.Count) _style = Program.Styles.Count - 1; + if (_style < 0) _style = 0; + ReloadStyle(); + if (dat.creationTime > new DateTime(2014, 1, 1)) { this.CreationTime = dat.creationTime; } + this.ResumeLayout(true); + _isLoading = false; + Save(); this.Show(); - return; + if (dat.hidden) this.Hide(); + } + finally + { + _isLoading = false; } - - //load others - this.SuspendLayout(); - this.StartPosition = FormStartPosition.Manual; - this.Location = dat.Location; - this.Size = dat.FormSize; - this.notebox1.Rtf = dat.data; - this.Opacity = dat.opacity; - this.Title = dat.title; - if (dat.customTheme != null) { this.CustomTheme = dat.customTheme; } - this.CurrentTheme = dat.theme; - if (dat.customStyle != null) { this.CustomStyle = dat.customStyle; } - this.CurrentStyle = dat.currentStyle; - if (dat.creationTime > new DateTime(2014, 1, 1)) { this.CreationTime = dat.creationTime; } - this.ResumeLayout(true); - - this.Show(); - if (dat.hidden) this.Hide(); } // // Properties and Variables // + private bool _isLoading = false; public DateTime CreationTime { get; set; } public int FORM_ID { get; set; } public string Title @@ -217,6 +238,7 @@ private void TopBar_MouseDown(object sender, System.Windows.Forms.MouseEventArgs // public bool Save() { + if (_isLoading) return true; try { REGISTRY.SetData(FORM_ID.ToString(), new FormData(this)); diff --git a/Desktop Notes/Desktop Notes/REGISTRY.cs b/Desktop Notes/Desktop Notes/REGISTRY.cs index 24ad7e0..aac0b35 100644 --- a/Desktop Notes/Desktop Notes/REGISTRY.cs +++ b/Desktop Notes/Desktop Notes/REGISTRY.cs @@ -1,50 +1,226 @@ -using Microsoft.Win32; +using Microsoft.Win32; using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; using System.Windows.Forms; namespace Desktop_Notes { public static class REGISTRY { + // 数据存储目录:%AppData%\Desktop_Notes\ + private static readonly string DATA_DIR = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), + "Desktop_Notes"); - public static RegistryKey REG_PATH = Registry.CurrentUser.CreateSubKey("Software") - .CreateSubKey("Sand Soft").CreateSubKey("Desktop Notes"); + private static readonly string NOTES_DIR = Path.Combine(DATA_DIR, "notes"); + private const string DEFAULT_SETTINGS_FILE = "_DefaultSettings.json"; + + // 旧版注册表路径(用于迁移存量用户数据) + private const string OLD_REG_PATH = @"Software\Sand Soft\Desktop Notes"; + + static REGISTRY() + { + // 确保数据目录存在 + if (!Directory.Exists(DATA_DIR)) + Directory.CreateDirectory(DATA_DIR); + if (!Directory.Exists(NOTES_DIR)) + Directory.CreateDirectory(NOTES_DIR); + + // 检测并迁移存量用户的注册表数据 + MigrateFromRegistry(); + } + + #region 注册表数据迁移 + + /// + /// 检测注册表中是否有旧版数据,如果有则迁移到文件存储 + /// + private static void MigrateFromRegistry() + { + try + { + RegistryKey regKey = Registry.CurrentUser.OpenSubKey(OLD_REG_PATH, true); + if (regKey == null) return; + + string[] valueNames = regKey.GetValueNames(); + if (valueNames.Length == 0) + { + regKey.Close(); + return; + } + + bool hasMigratedData = false; + + foreach (string name in valueNames) + { + try + { + object val = regKey.GetValue(name, null); + if (val == null) continue; + string json = (string)val; + + if (name == "_DefaultSettings") + { + // 迁移默认设置 + string settingsPath = Path.Combine(DATA_DIR, DEFAULT_SETTINGS_FILE); + if (!File.Exists(settingsPath)) + { + File.WriteAllText(settingsPath, json); + hasMigratedData = true; + } + } + else if (!name.StartsWith("_")) + { + // 迁移便签数据 + string notePath = Path.Combine(NOTES_DIR, name + ".json"); + if (!File.Exists(notePath)) + { + File.WriteAllText(notePath, json); + hasMigratedData = true; + } + } + } + catch { } + } + + // 迁移成功后清理注册表中的旧数据 + if (hasMigratedData) + { + foreach (string name in valueNames) + { + try { regKey.DeleteValue(name, false); } + catch { } + } + } + + regKey.Close(); + + // 尝试清理空的注册表键 + try + { + RegistryKey parentKey = Registry.CurrentUser.OpenSubKey(@"Software\Sand Soft", true); + if (parentKey != null) + { + RegistryKey check = parentKey.OpenSubKey("Desktop Notes"); + if (check != null && check.GetValueNames().Length == 0 && check.SubKeyCount == 0) + { + check.Close(); + parentKey.DeleteSubKey("Desktop Notes", false); + } + else if (check != null) + { + check.Close(); + } + parentKey.Close(); + } + } + catch { } + } + catch { } + } + + #endregion + + #region 便签数据存储(文件) public static string[] OPENED_NOTES { get { - return REG_PATH.GetValueNames(); + List notes = new List(); + if (!Directory.Exists(NOTES_DIR)) return notes.ToArray(); + + foreach (string file in Directory.GetFiles(NOTES_DIR, "*.json")) + { + string name = Path.GetFileNameWithoutExtension(file); + notes.Add(name); + } + return notes.ToArray(); } } public static void SetData(string id, FormData data) { - string dat = JsonConvert.SerializeObject(data); - REG_PATH.SetValue(id, dat); + try + { + string json = JsonConvert.SerializeObject(data, Formatting.Indented); + string path = Path.Combine(NOTES_DIR, id + ".json"); + File.WriteAllText(path, json); + } + catch { } } public static FormData GetData(string id) { - object dat = REG_PATH.GetValue(id, null); - if (dat == null) return null; - return JsonConvert.DeserializeObject((string)dat); + try + { + string path = Path.Combine(NOTES_DIR, id + ".json"); + if (!File.Exists(path)) return null; + string json = File.ReadAllText(path); + return JsonConvert.DeserializeObject(json); + } + catch { return null; } } public static void Delete(string id) { - try { REG_PATH.DeleteValue(id); } + try + { + string path = Path.Combine(NOTES_DIR, id + ".json"); + if (File.Exists(path)) File.Delete(path); + } catch { } } public static void DeleteAll() { - foreach (string val in OPENED_NOTES) + try + { + if (!Directory.Exists(NOTES_DIR)) return; + foreach (string file in Directory.GetFiles(NOTES_DIR, "*.json")) + { + File.Delete(file); + } + } + catch { } + } + + #endregion + + #region 默认便签设置(文件) + + public static void SaveDefaultSettings(DefaultNoteSettings settings) + { + try { - REG_PATH.DeleteValue(val); + string json = JsonConvert.SerializeObject(settings, Formatting.Indented); + string path = Path.Combine(DATA_DIR, DEFAULT_SETTINGS_FILE); + File.WriteAllText(path, json); } + catch { } } + public static DefaultNoteSettings GetDefaultSettings() + { + try + { + string path = Path.Combine(DATA_DIR, DEFAULT_SETTINGS_FILE); + if (!File.Exists(path)) return new DefaultNoteSettings(); + string json = File.ReadAllText(path); + return JsonConvert.DeserializeObject(json); + } + catch + { + return new DefaultNoteSettings(); + } + } + + #endregion + + #region 系统设置(仍使用注册表) + public static RegistryKey START_KEY = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); public static bool StartWithWindows { @@ -76,33 +252,6 @@ public static bool FirstRun } } - // 默认便签设置的注册表键名 - private const string DEFAULT_SETTINGS_KEY = "_DefaultSettings"; - - // 保存默认便签设置 - public static void SaveDefaultSettings(DefaultNoteSettings settings) - { - try - { - string json = JsonConvert.SerializeObject(settings); - REG_PATH.SetValue(DEFAULT_SETTINGS_KEY, json); - } - catch { } - } - - // 读取默认便签设置 - public static DefaultNoteSettings GetDefaultSettings() - { - try - { - object dat = REG_PATH.GetValue(DEFAULT_SETTINGS_KEY, null); - if (dat == null) return new DefaultNoteSettings(); - return JsonConvert.DeserializeObject((string)dat); - } - catch - { - return new DefaultNoteSettings(); - } - } + #endregion } } diff --git a/Product.wxs b/Product.wxs index 3dfcee9..b569bb9 100644 --- a/Product.wxs +++ b/Product.wxs @@ -21,14 +21,13 @@ - - + @@ -60,12 +59,12 @@ - + - + - +