diff --git a/86BoxManager/FolderSelectDialog.cs b/86BoxManager/FolderSelectDialog.cs index b01a9be..af59232 100644 --- a/86BoxManager/FolderSelectDialog.cs +++ b/86BoxManager/FolderSelectDialog.cs @@ -1,5 +1,4 @@ -#if !NETCOREAPP // Not required for .NET Core builds -using System; +using System; using System.Reflection; using System.Windows.Forms; @@ -108,5 +107,4 @@ private class WindowWrapper : IWin32Window public IntPtr Handle { get { return _handle; } } } } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/86BoxManager/dlgAddVM.cs b/86BoxManager/dlgAddVM.cs index 25aa7ae..a2a4f05 100644 --- a/86BoxManager/dlgAddVM.cs +++ b/86BoxManager/dlgAddVM.cs @@ -71,25 +71,6 @@ private void txtName_TextChanged(object sender, EventArgs e) } } -// .NET Core implements the better Vista-style folder browse dialog in the stock FolderBrowserDialog -#if NETCOREAPP - private void btnBrowse_Click(object sender, EventArgs e) - { - FolderBrowserDialog dialog = new FolderBrowserDialog - { - RootFolder = Environment.SpecialFolder.MyComputer, - Description = "Select a folder where your virtual machines (configs, nvr folders, etc.) will be located", - UseDescriptionForTitle = true - }; - - if (dialog.ShowDialog() == DialogResult.OK) - { - txtImportPath.Text = dialog.SelectedPath; - txtName.Text = Path.GetFileName(dialog.SelectedPath); - } - } -// A custom class is required for Vista-style folder dialogs under the original .NET Framework -#else private void btnBrowse_Click(object sender, EventArgs e) { FolderSelectDialog dialog = new FolderSelectDialog @@ -104,7 +85,6 @@ private void btnBrowse_Click(object sender, EventArgs e) txtName.Text = Path.GetFileName(dialog.FileName); } } -#endif private void cbxImport_CheckedChanged(object sender, EventArgs e) { diff --git a/86BoxManager/dlgSettings.cs b/86BoxManager/dlgSettings.cs index 5340c92..cdceb56 100644 --- a/86BoxManager/dlgSettings.cs +++ b/86BoxManager/dlgSettings.cs @@ -220,6 +220,8 @@ private void LoadSettings() } catch (Exception ex) { + MessageBox.Show("86Box Manager settings could not be loaded, because an error occured trying to load the registry keys and/or values. Make sure you have the required permissions and try again. Default values will be used now.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); + txtCFGdir.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\86Box VMs"; txtEXEdir.Text = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + @"\86Box"; cbxMinimize.Checked = false; @@ -234,47 +236,6 @@ private void LoadSettings() } } -// .NET Core implements the better Vista-style folder browse dialog in the stock FolderBrowserDialog -#if NETCOREAPP - private void btnBrowse1_Click(object sender, EventArgs e) - { - FolderBrowserDialog dialog = new FolderBrowserDialog - { - RootFolder = Environment.SpecialFolder.MyComputer, - Description = "Select a folder where 86Box program files and the roms folder are located", - UseDescriptionForTitle = true - }; - - if (dialog.ShowDialog() == DialogResult.OK) - { - txtEXEdir.Text = dialog.SelectedPath; - if (!txtEXEdir.Text.EndsWith(@"\")) //Just in case - { - txtEXEdir.Text += @"\"; - } - } - } - - private void btnBrowse2_Click(object sender, EventArgs e) - { - FolderBrowserDialog dialog = new FolderBrowserDialog - { - RootFolder = Environment.SpecialFolder.MyComputer, - Description = "Select a folder where your virtual machines (configs, nvr folders, etc.) will be located", - UseDescriptionForTitle = true - }; - - if (dialog.ShowDialog() == DialogResult.OK) - { - txtCFGdir.Text = dialog.SelectedPath; - if (!txtCFGdir.Text.EndsWith(@"\")) //Just in case - { - txtCFGdir.Text += @"\"; - } - } - } -// A custom class is required for Vista-style folder dialogs under the original .NET Framework -#else private void btnBrowse1_Click(object sender, EventArgs e) { FolderSelectDialog dialog = new FolderSelectDialog @@ -310,7 +271,6 @@ private void btnBrowse2_Click(object sender, EventArgs e) } } } -#endif private void btnDefaults_Click(object sender, EventArgs e) { diff --git a/86BoxManager/frmMain.cs b/86BoxManager/frmMain.cs index 330e286..fbe586c 100644 --- a/86BoxManager/frmMain.cs +++ b/86BoxManager/frmMain.cs @@ -1,7 +1,5 @@ -#if !NETCOREAPP // COM references require .NET framework for now -using _86boxManager.Properties; +using _86boxManager.Properties; using IWshRuntimeLibrary; -#endif using Microsoft.Win32; using System; using System.ComponentModel; @@ -33,7 +31,7 @@ public struct COPYDATASTRUCT public IntPtr lpData; } - private static RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box", true); //Registry key for accessing the settings and VM list + //private static RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box", true); //Registry key for accessing the settings and VM list public string exepath = ""; //Path to 86box.exe and the romset public string cfgpath = ""; //Path to the virtual machines folder (configs, nvrs, etc.) private bool minimize = false; //Minimize the main window when a VM is started? @@ -50,10 +48,6 @@ public struct COPYDATASTRUCT public frmMain() { InitializeComponent(); - -#if NETCOREAPP - createADesktopShortcutToolStripMenuItem.Enabled = false; // Requires the original .NET framework -#endif } private void frmMain_Load(object sender, EventArgs e) @@ -222,69 +216,69 @@ private void btnEdit_Click(object sender, EventArgs e) //Load the settings from the registry private void LoadSettings() { - regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box", true); - - //Try to load the settings from registry, if it fails fallback to default values try { - exepath = regkey.GetValue("EXEdir").ToString(); - cfgpath = regkey.GetValue("CFGdir").ToString(); - minimize = Convert.ToBoolean(regkey.GetValue("MinimizeOnVMStart")); - showConsole = Convert.ToBoolean(regkey.GetValue("ShowConsole")); - minimizeTray = Convert.ToBoolean(regkey.GetValue("MinimizeToTray")); - closeTray = Convert.ToBoolean(regkey.GetValue("CloseToTray")); - logpath = regkey.GetValue("LogPath").ToString(); - logging = Convert.ToBoolean(regkey.GetValue("EnableLogging")); - gridlines = Convert.ToBoolean(regkey.GetValue("EnableGridLines")); - sortColumn = (int)regkey.GetValue("SortColumn"); - sortOrder = (SortOrder)regkey.GetValue("SortOrder"); - - lstVMs.GridLines = gridlines; - VMSort(sortColumn, sortOrder); - } - catch (Exception ex) - { - MessageBox.Show("86Box Manager settings could not be loaded. This is normal if you're running 86Box Manager for the first time. Default values will be used.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); + //Try to load the settings from registry, if it fails fallback to default values + RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box", true); - //If the key doesn't exist, create it and then reopen it if (regkey == null) { + MessageBox.Show("86Box Manager settings could not be loaded. This is normal if you're running 86Box Manager for the first time. Default values will be used.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); + + //Create the key and reopen it for write access Registry.CurrentUser.CreateSubKey(@"SOFTWARE\86Box"); regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box", true); regkey.CreateSubKey("Virtual Machines"); + + cfgpath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\86Box VMs\"; + exepath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + @"\86Box\"; + minimize = false; + showConsole = true; + minimizeTray = false; + closeTray = false; + logging = false; + logpath = ""; + gridlines = false; + sortColumn = 0; + sortOrder = SortOrder.Ascending; + + lstVMs.GridLines = false; + VMSort(sortColumn, sortOrder); + + //Defaults must also be written to the registry + regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box", true); + regkey.SetValue("EXEdir", exepath, RegistryValueKind.String); + regkey.SetValue("CFGdir", cfgpath, RegistryValueKind.String); + regkey.SetValue("MinimizeOnVMStart", minimize, RegistryValueKind.DWord); + regkey.SetValue("ShowConsole", showConsole, RegistryValueKind.DWord); + regkey.SetValue("MinimizeToTray", minimizeTray, RegistryValueKind.DWord); + regkey.SetValue("CloseToTray", closeTray, RegistryValueKind.DWord); + regkey.SetValue("EnableLogging", logging, RegistryValueKind.DWord); + regkey.SetValue("LogPath", logpath, RegistryValueKind.String); + regkey.SetValue("EnableGridLines", gridlines, RegistryValueKind.DWord); + regkey.SetValue("SortColumn", sortColumn, RegistryValueKind.DWord); + regkey.SetValue("SortOrder", sortOrder, RegistryValueKind.DWord); + } + else + { + exepath = regkey.GetValue("EXEdir").ToString(); + cfgpath = regkey.GetValue("CFGdir").ToString(); + minimize = Convert.ToBoolean(regkey.GetValue("MinimizeOnVMStart")); + showConsole = Convert.ToBoolean(regkey.GetValue("ShowConsole")); + minimizeTray = Convert.ToBoolean(regkey.GetValue("MinimizeToTray")); + closeTray = Convert.ToBoolean(regkey.GetValue("CloseToTray")); + logpath = regkey.GetValue("LogPath").ToString(); + logging = Convert.ToBoolean(regkey.GetValue("EnableLogging")); + gridlines = Convert.ToBoolean(regkey.GetValue("EnableGridLines")); + sortColumn = (int)regkey.GetValue("SortColumn"); + sortOrder = (SortOrder)regkey.GetValue("SortOrder"); + + lstVMs.GridLines = gridlines; + VMSort(sortColumn, sortOrder); } - cfgpath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + @"\86Box VMs\"; - exepath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + @"\86Box\"; - minimize = false; - showConsole = true; - minimizeTray = false; - closeTray = false; - logging = false; - logpath = ""; - gridlines = false; - sortColumn = 0; - sortOrder = SortOrder.Ascending; - - lstVMs.GridLines = false; - VMSort(sortColumn, sortOrder); - - //Defaults must also be written to the registry - regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box", true); - regkey.SetValue("EXEdir", exepath, RegistryValueKind.String); - regkey.SetValue("CFGdir", cfgpath, RegistryValueKind.String); - regkey.SetValue("MinimizeOnVMStart", minimize, RegistryValueKind.DWord); - regkey.SetValue("ShowConsole", showConsole, RegistryValueKind.DWord); - regkey.SetValue("MinimizeToTray", minimizeTray, RegistryValueKind.DWord); - regkey.SetValue("CloseToTray", closeTray, RegistryValueKind.DWord); - regkey.SetValue("EnableLogging", logging, RegistryValueKind.DWord); - regkey.SetValue("LogPath", logpath, RegistryValueKind.String); - regkey.SetValue("EnableGridLines", gridlines, RegistryValueKind.DWord); - regkey.SetValue("SortColumn", sortColumn, RegistryValueKind.DWord); - regkey.SetValue("SortOrder", sortOrder, RegistryValueKind.DWord); - } - finally - { + regkey.Close(); + //To make sure there's a trailing backslash at the end, as other code using these strings expects it! if (!exepath.EndsWith(@"\")) { @@ -295,8 +289,11 @@ private void LoadSettings() cfgpath += @"\"; } } - - regkey.Close(); + catch (Exception ex) + { + MessageBox.Show("An error occured trying to load the 86Box Manager registry keys and/or values. Make sure you have the required permissions and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + Application.Exit(); + } } //TODO: Rewrite @@ -307,7 +304,7 @@ private void LoadVMs() VMCountRefresh(); try { - regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box\Virtual Machines"); + RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box\Virtual Machines"); VM vm = new VM(); foreach (var value in regkey.GetValueNames()) @@ -921,7 +918,8 @@ public void VMAdd(string name, string desc, bool openCFG, bool startVM) var formatter = new BinaryFormatter(); formatter.Serialize(ms, newVM); var data = ms.ToArray(); - regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box\Virtual Machines", true); + + RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box\Virtual Machines", true); regkey.SetValue(newVM.Name, data, RegistryValueKind.Binary); } @@ -952,24 +950,32 @@ public void VMAdd(string name, string desc, bool openCFG, bool startVM) //Checks if a VM with this name already exists public bool VMCheckIfExists(string name) { - regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box\Virtual Machines", true); - if (regkey == null) //Regkey doesn't exist yet + try { - regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box", true); - regkey.CreateSubKey(@"Virtual Machines"); - return false; - } + RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box\Virtual Machines", true); + if (regkey == null) //Regkey doesn't exist yet + { + regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box", true); + regkey.CreateSubKey(@"Virtual Machines"); + return false; + } - //VM's registry value doesn't exist yet - if (regkey.GetValue(name) == null) - { - regkey.Close(); - return false; + //VM's registry value doesn't exist yet + if (regkey.GetValue(name) == null) + { + regkey.Close(); + return false; + } + else + { + regkey.Close(); + return true; + } } - else + catch(Exception ex) { - regkey.Close(); - return true; + MessageBox.Show("Could not load the virtual machine information from the registry. Make sure you have the required permissions and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return false; } } @@ -994,7 +1000,7 @@ public void VMEdit(string name, string desc) vm.Desc = desc; //Create a new registry value with new info, delete the old one - regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box\Virtual Machines", true); + RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box\Virtual Machines", true); using (var ms = new MemoryStream()) { regkey.DeleteValue(oldname); @@ -1034,7 +1040,7 @@ private void VMRemove() try { lstVMs.Items.Remove(lvi); - regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box\Virtual Machines", true); + RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box\Virtual Machines", true); regkey.DeleteValue(vm.Name); regkey.Close(); } @@ -1367,7 +1373,6 @@ private void VMOpenFolder() private void createADesktopShortcutToolStripMenuItem_Click(object sender, EventArgs e) { -#if !NETCOREAPP // Requires the original .NET Framework foreach (ListViewItem lvi in lstVMs.SelectedItems) { VM vm = (VM)lvi.Tag; @@ -1389,7 +1394,6 @@ private void createADesktopShortcutToolStripMenuItem_Click(object sender, EventA MessageBox.Show("A desktop shortcut for the virtual machine \"" + vm.Name + "\" could not be created.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } -#endif } //Starts/stops selected VM when enter is pressed @@ -1594,10 +1598,17 @@ private void VMSort(int column, SortOrder order) lstVMs.ListViewItemSorter = new ListViewItemComparer(sortColumn, sortOrder); //Save the new column and order to the registry - regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box", true); - regkey.SetValue("SortColumn", sortColumn, RegistryValueKind.DWord); - regkey.SetValue("SortOrder", sortOrder, RegistryValueKind.DWord); - regkey.Close(); + try + { + RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box", true); + regkey.SetValue("SortColumn", sortColumn, RegistryValueKind.DWord); + regkey.SetValue("SortOrder", sortOrder, RegistryValueKind.DWord); + regkey.Close(); + } + catch(Exception ex) + { + MessageBox.Show("Could not save the column sorting state to the registry. Make sure you have the required permissions and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } //Handles the click event for the listview column headers, allowing to sort the items by columns @@ -1687,7 +1698,7 @@ public void VMImport(string name, string desc, string importPath, bool openCFG, var formatter = new BinaryFormatter(); formatter.Serialize(ms, newVM); var data = ms.ToArray(); - regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box\Virtual Machines", true); + RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\86Box\Virtual Machines", true); regkey.SetValue(newVM.Name, data, RegistryValueKind.Binary); }