using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.Win32; namespace FileWatcher { public sealed partial class Preferences : Form { #region Singleton private static readonly Preferences instance = new Preferences(); static Preferences() { } public static Preferences Instance { get { return instance; } } #endregion private class ProgramData { public string Path; public bool Save; public ProgramData(string path, bool save = false) { Path = path; Save = save; } } private Dictionary programs = new Dictionary(); private BindingList UnboundBindingList = null; private Preferences() { InitializeComponent(); // TODO: load preferences UnboundBindingList = new BindingList(new List(programs.Keys)); listBox1.DataSource = UnboundBindingList; RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); SetRunAtStartup(registryKey.GetValue(Form1.ApplicationName) != null); LoadEditorPreferences(new List(), new List>()); } public void LoadEditorPreferences(List defaultPrograms, List> userDefinedPrograms) { programs.Clear(); #region Get System Default Text Editor //http://stackoverflow.com/questions/11365984/c-sharp-open-file-with-default-application-and-parameters string textEditorDefault = ""; string InstallPath = (string)Registry.GetValue(@"HKEY_CLASSES_ROOT\.txt", "", null); if (InstallPath != null) { string commandPath = (string)Registry.GetValue(@"HKEY_CLASSES_ROOT\\" + InstallPath + "\\Shell\\Open\\Command", "", null); string ext = ".exe"; int indexOf = commandPath.ToLower().LastIndexOf(ext); if (indexOf > 0) { // strip off arguments (%1,%2,etc) textEditorDefault = commandPath.Substring(0, indexOf + ext.Length); } } #endregion string pfx64 = Utils.ProgramFilesx64(); string pfx86 = Utils.ProgramFilesx86(); string sys = Utils.SystemDirectory(); // Add system default options first so the user options can overwrite these if (File.Exists(textEditorDefault)) { programs.Add("System Default", new ProgramData(textEditorDefault)); } if (File.Exists(pfx64 + "\\Sublime Text 3\\sublime_text.exe")) { programs.Add("Sublime Text 3", new ProgramData(pfx64 + "\\Sublime Text 3\\sublime_text.exe")); } if (File.Exists(pfx64 + "\\Sublime Text 2\\sublime_text.exe")) { programs.Add("Sublime Text 2", new ProgramData(pfx64 + "\\Sublime Text 2\\sublime_text.exe")); } if (File.Exists(pfx64 + "\\Sublime Text\\sublime_text.exe")) { programs.Add("Sublime Text", new ProgramData(pfx64 + "\\Sublime Text\\sublime_text.exe")); } if (File.Exists(pfx86 + "\\Notepad++\\notepad++.exe")) { programs.Add("Notepad++", new ProgramData(pfx86 + "\\Notepad++\\notepad++.exe")); } if (File.Exists(sys + "\\notepad.exe")) { programs.Add("NotePad", new ProgramData(sys + "\\notepad.exe")); } foreach (Tuple t in userDefinedPrograms) { programs.Add(t.Item1, new ProgramData(t.Item2,true)); if (File.Exists(t.Item2) == false) { Logger.WriteLine("A program set as one of the default text editors does not exist on your system: " + t.Item2); } } defaultPrograms.RemoveAll(str => !programs.ContainsKey(str) || !File.Exists(programs[str].Path)); UnboundBindingList = new BindingList(defaultPrograms); foreach (string str in programs.Keys) { if (defaultPrograms.Contains(str) == false) { defaultPrograms.Add(str); } } listBox1.DataSource = UnboundBindingList; } public string GetDefaultEditor() { foreach (string programName in UnboundBindingList) { string path = programs[programName].Path; if (File.Exists(path)) { return path; } } return ""; } public List GetDefaultPrograms() { return new List(UnboundBindingList); } // Gets user defined paths public List> GetEditorProgramPaths() { List> programNames = new List>(); foreach (KeyValuePair t in programs) { if (t.Value.Save) { programNames.Add(new Tuple(t.Key, t.Value.Path)); } } return programNames; } protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } private void saveButton_Click(object sender, EventArgs e) { Form1.Instance.Save(); } private void cancelButton_Click(object sender, EventArgs e) { } private void listBox1_SelectedValueChanged(object sender, EventArgs e) { if (listBox1.SelectedValue != null) { string selectedValue = (string)listBox1.SelectedValue; if (programs.ContainsKey(selectedValue)) { string path = programs[(string)listBox1.SelectedValue].Path; textBox1.Text = path; } } } private void Preferences_FormClosing(object sender, FormClosingEventArgs e) { this.Hide(); e.Cancel = true; } private void toolStripButton3_Click(object sender, EventArgs e) { } private void toolStripButton4_Click(object sender, EventArgs e) { } private void toolStripButton1_Click(object sender, EventArgs e) { int selectedIndex = listBox1.SelectedIndex; if (selectedIndex >= listBox1.Items.Count - 1) { return; } listBox1.DataSource = null; listBox1.SelectionMode = SelectionMode.None; string obj1 = UnboundBindingList[selectedIndex]; UnboundBindingList[selectedIndex] = UnboundBindingList[selectedIndex + 1]; UnboundBindingList[selectedIndex + 1] = obj1; listBox1.DataSource = UnboundBindingList; listBox1.SelectionMode = SelectionMode.One; listBox1.SelectedIndex = selectedIndex + 1; } private void toolStripButton2_Click(object sender, EventArgs e) { int selectedIndex = listBox1.SelectedIndex; if (selectedIndex == 0) { return; } listBox1.DataSource = null; listBox1.SelectionMode = SelectionMode.None; string obj1 = UnboundBindingList[selectedIndex]; UnboundBindingList[selectedIndex] = UnboundBindingList[selectedIndex - 1]; UnboundBindingList[selectedIndex - 1] = obj1; listBox1.DataSource = UnboundBindingList; listBox1.SelectionMode = SelectionMode.One; listBox1.SelectedIndex = selectedIndex - 1; } public void SetRunAtStartup(bool yes) { RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (registryKey == null) { return; } if (yes) { registryKey.SetValue(Form1.ApplicationName, Application.ExecutablePath + " --minimize"); } else { if (registryKey.GetValue(Form1.ApplicationName) != null) { registryKey.DeleteValue(Form1.ApplicationName); } } runAtStartupCB.Checked = yes; } private void runAtStartupCB_CheckedChanged(object sender, EventArgs e) { SetRunAtStartup(runAtStartupCB.Checked); } private void Preferences_Load(object sender, EventArgs e) { } } }