From 45fe4084ee683fca17c792fde977f1b8bbebe545 Mon Sep 17 00:00:00 2001 From: Markus Hofknecht Date: Sat, 24 Oct 2020 13:12:59 +0200 Subject: [PATCH] [BUG] Fix get correct network path if it contains spaces (#140), version 1.0.17.1 --- Business/Menus.cs | 7 ++-- NativeDllImport/ShowWindow.cs | 4 +-- Properties/AssemblyInfo.cs | 4 +-- Properties/CustomSettingsProvider.cs | 51 ++++++++++++---------------- UserInterface/Menu.cs | 1 - Utilities/FolderOptions.cs | 12 +++++-- 6 files changed, 40 insertions(+), 39 deletions(-) diff --git a/Business/Menus.cs b/Business/Menus.cs index 34c0c83..2b2d61b 100644 --- a/Business/Menus.cs +++ b/Business/Menus.cs @@ -310,12 +310,13 @@ namespace SystemTrayMenu.Business { foreach (string line in lines.Skip(6).SkipLast(2)) { - int indexOfFirstSpace = line.IndexOf(" ", StringComparison.InvariantCulture); + int indexOfFirstSpace = line.TrimEnd().LastIndexOf(" ", StringComparison.InvariantCulture); if (indexOfFirstSpace > 0) { string directory = Path.Combine( - networkLocationRootPath, - line.Substring(0, indexOfFirstSpace)); + networkLocationRootPath, + line.Substring(0, indexOfFirstSpace)).TrimEnd(); + directories.Add(directory); resolvedSomething = true; } diff --git a/NativeDllImport/ShowWindow.cs b/NativeDllImport/ShowWindow.cs index 2f144be..478fcc1 100644 --- a/NativeDllImport/ShowWindow.cs +++ b/NativeDllImport/ShowWindow.cs @@ -12,8 +12,8 @@ namespace SystemTrayMenu.DllImports /// public static partial class NativeMethods { - [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] - private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); + private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); } } diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index e1ce45a..9add406 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -39,5 +39,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.17.0")] -[assembly: AssemblyFileVersion("1.0.17.0")] +[assembly: AssemblyVersion("1.0.17.1")] +[assembly: AssemblyFileVersion("1.0.17.1")] diff --git a/Properties/CustomSettingsProvider.cs b/Properties/CustomSettingsProvider.cs index 205607f..7d487a7 100644 --- a/Properties/CustomSettingsProvider.cs +++ b/Properties/CustomSettingsProvider.cs @@ -36,10 +36,7 @@ namespace SystemTrayMenu.Properties /// public override string ApplicationName { - get - { - return System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name; - } + get => System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name; set { @@ -51,17 +48,11 @@ namespace SystemTrayMenu.Properties /// Gets the setting key this is returning must set before the settings are used. /// e.g. Properties.Settings.Default.SettingsKey = @"C:\temp\user.config";. /// - private static string UserConfigPath - { - get - { - return Path.Combine( + private static string UserConfigPath => Path.Combine( Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), $"SystemTrayMenu"), $"user-{Environment.MachineName}.config"); - } - } /// /// Gets or sets in memory storage of the settings values. @@ -99,11 +90,13 @@ namespace SystemTrayMenu.Properties // itterate thought the properties we get from the designer, checking to see if the setting is in the dictionary foreach (SettingsProperty setting in collection) { - SettingsPropertyValue value = new SettingsPropertyValue(setting); - value.IsDirty = false; + SettingsPropertyValue value = new SettingsPropertyValue(setting) + { + IsDirty = false, + }; // need the type of the value for the strong typing - var t = Type.GetType(setting.PropertyType.FullName); + Type t = Type.GetType(setting.PropertyType.FullName); if (SettingsDictionary.ContainsKey(setting.Name)) { @@ -133,7 +126,7 @@ namespace SystemTrayMenu.Properties // grab the values from the collection parameter and update the values in our dictionary. foreach (SettingsPropertyValue value in collection) { - var setting = new SettingStruct() + SettingStruct setting = new SettingStruct() { Value = value.PropertyValue == null ? string.Empty : value.PropertyValue.ToString(), Name = value.Name, @@ -160,11 +153,11 @@ namespace SystemTrayMenu.Properties /// private static void CreateEmptyConfig() { - var doc = new XDocument(); - var declaration = new XDeclaration("1.0", "utf-8", "true"); - var config = new XElement(Config); - var userSettings = new XElement(UserSettings); - var group = new XElement(typeof(Properties.Settings).FullName); + XDocument doc = new XDocument(); + XDeclaration declaration = new XDeclaration("1.0", "utf-8", "true"); + XElement config = new XElement(Config); + XElement userSettings = new XElement(UserSettings); + XElement group = new XElement(typeof(Properties.Settings).FullName); userSettings.Add(group); config.Add(userSettings); doc.Add(config); @@ -184,16 +177,16 @@ namespace SystemTrayMenu.Properties } // load the xml - var configXml = XDocument.Load(UserConfigPath); + XDocument configXml = XDocument.Load(UserConfigPath); // get all of the elements. - var settingElements = configXml.Element(Config).Element(UserSettings).Element(typeof(Properties.Settings).FullName).Elements(Setting); + IEnumerable settingElements = configXml.Element(Config).Element(UserSettings).Element(typeof(Properties.Settings).FullName).Elements(Setting); // iterate through, adding them to the dictionary, (checking for nulls, xml no likey nulls) // using "String" as default serializeAs...just in case, no real good reason. - foreach (var element in settingElements) + foreach (XElement element in settingElements) { - var newSetting = new SettingStruct() + SettingStruct newSetting = new SettingStruct() { Name = element.Attribute(NameOf) == null ? string.Empty : element.Attribute(NameOf).Value, SerializeAs = element.Attribute(SerializeAs) == null ? "String" : element.Attribute(SerializeAs).Value, @@ -209,19 +202,19 @@ namespace SystemTrayMenu.Properties private void SaveValuesToFile() { // load the current xml from the file. - var import = XDocument.Load(UserConfigPath); + XDocument import = XDocument.Load(UserConfigPath); // get the settings group (e.g. ) - var settingsSection = import.Element(Config).Element(UserSettings).Element(typeof(Properties.Settings).FullName); + XElement settingsSection = import.Element(Config).Element(UserSettings).Element(typeof(Properties.Settings).FullName); // iterate though the dictionary, either updating the value or adding the new setting. - foreach (var entry in SettingsDictionary) + foreach (KeyValuePair entry in SettingsDictionary) { - var setting = settingsSection.Elements().FirstOrDefault(e => e.Attribute(NameOf).Value == entry.Key); + XElement setting = settingsSection.Elements().FirstOrDefault(e => e.Attribute(NameOf).Value == entry.Key); if (setting == null) { // this can happen if a new setting is added via the .settings designer. - var newSetting = new XElement(Setting); + XElement newSetting = new XElement(Setting); newSetting.Add(new XAttribute(NameOf, entry.Value.Name)); newSetting.Add(new XAttribute(SerializeAs, entry.Value.SerializeAs)); newSetting.Value = entry.Value.Value ?? string.Empty; diff --git a/UserInterface/Menu.cs b/UserInterface/Menu.cs index 55854ef..c7efc87 100644 --- a/UserInterface/Menu.cs +++ b/UserInterface/Menu.cs @@ -11,7 +11,6 @@ namespace SystemTrayMenu.UserInterface using System.Linq; using System.Reflection; using System.Windows.Forms; - using Microsoft.Win32; using SystemTrayMenu.DataClasses; using SystemTrayMenu.DllImports; using SystemTrayMenu.Utilities; diff --git a/Utilities/FolderOptions.cs b/Utilities/FolderOptions.cs index a983a2c..a242124 100644 --- a/Utilities/FolderOptions.cs +++ b/Utilities/FolderOptions.cs @@ -74,9 +74,17 @@ namespace SystemTrayMenu.Utilities isDirectoryToHide = true; } } - catch (UnauthorizedAccessException ex) + catch (Exception ex) { - Log.Warn($"path:'{path}'", ex); + if (ex is UnauthorizedAccessException || + ex is IOException) + { + Log.Warn($"path:'{path}'", ex); + } + else + { + throw; + } } } else