diff --git a/Controls/RowData.cs b/Controls/RowData.cs index ea7e194..f805896 100644 --- a/Controls/RowData.cs +++ b/Controls/RowData.cs @@ -116,7 +116,7 @@ namespace SystemTrayMenu.Controls else if (string.IsNullOrEmpty(resolvedLnkPath)) { ResolvedFileNotFound = true; - Log.Info($"Resolve *.Lnk '{TargetFilePath}' empty => no icon"); + Log.Info($"Resolve *.LNK '{TargetFilePath}' has no icon"); #warning [Feature] Resolve network root #48, start here } else @@ -181,7 +181,7 @@ namespace SystemTrayMenu.Controls } else { - Log.Info($"Resolve *.URL '{TargetFilePath}' not possible => no icon"); + Log.Info($"Resolve *.URL '{TargetFilePath}' has no icon"); } } catch (Exception ex) diff --git a/Helper/FolderOptions.cs b/Helper/FolderOptions.cs new file mode 100644 index 0000000..3151c03 --- /dev/null +++ b/Helper/FolderOptions.cs @@ -0,0 +1,54 @@ +using Shell32; +using System; +using System.IO; + +namespace SystemTrayMenu.Helper +{ + internal static class FolderOptions + { + static bool hideHiddenEntries = false; + static bool hideSystemEntries = false; + static IShellDispatch4 iShellDispatch4 = null; + + internal static void Initialize() + { + try + { + iShellDispatch4 = (IShellDispatch4)Activator.CreateInstance( + Type.GetTypeFromProgID("Shell.Application")); + + // Using SHGetSetSettings would be much better in performance but the results are not accurate. + // We have to go for the shell interface in order to receive the correct settings: + // https://docs.microsoft.com/en-us/windows/win32/shell/ishelldispatch4-getsetting + const int SSF_SHOWALLOBJECTS = 0x00000001; + hideHiddenEntries = !iShellDispatch4.GetSetting( + SSF_SHOWALLOBJECTS); + + const int SSF_SHOWSUPERHIDDEN = 0x00040000; + hideSystemEntries = !iShellDispatch4.GetSetting( + SSF_SHOWSUPERHIDDEN); + } + catch (Exception ex) + { + Log.Error("Get Shell COM instance failed", ex); + } + } + + internal static bool IsHidden(string path, ref bool hiddenEntry) + { + bool isDirectoryToHide = false; + + FileAttributes attributes = File.GetAttributes(path); + hiddenEntry = attributes.HasFlag(FileAttributes.Hidden); + bool systemEntry = attributes.HasFlag( + FileAttributes.Hidden | FileAttributes.System); + if ((hideHiddenEntries && hiddenEntry) || + (hideSystemEntries && systemEntry)) + { + isDirectoryToHide = true; + } + + return isDirectoryToHide; + } + } +} diff --git a/Helper/Log.cs b/Helper/Log.cs index 1ae893f..45004a1 100644 --- a/Helper/Log.cs +++ b/Helper/Log.cs @@ -42,13 +42,13 @@ namespace SystemTrayMenu.Helper Process.Start(GetLogFilePath()); } - internal static void ApplicationStart() + internal static void ApplicationRun() { Assembly assembly = Assembly.GetExecutingAssembly(); log.Info($"Application Start " + - assembly.ManifestModule.FullyQualifiedName + "|" + - assembly.GetName().Version.ToString() + "|" + - $" ScalingFactor={Scaling.Factor}"); + assembly.ManifestModule.Name + " | " + + assembly.GetName().Version.ToString() + " | " + + $"ScalingFactor={Scaling.Factor}"); } } } diff --git a/Program.cs b/Program.cs index 4cfd426..d6e1036 100644 --- a/Program.cs +++ b/Program.cs @@ -19,6 +19,7 @@ namespace SystemTrayMenu SingleAppInstance.Initialize(); Language.Initialize(); + Config.UpgradeIfNotUpgraded(); if (Config.LoadOrSetByUser()) { Application.EnableVisualStyles(); @@ -30,9 +31,11 @@ namespace SystemTrayMenu } Scaling.Initialize(); + FolderOptions.Initialize(); using (new SystemTrayMenu()) { + Log.ApplicationRun(); Application.Run(); } } diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index a9e2245..03dcf55 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -31,5 +31,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("0.9.2.3")] -[assembly: AssemblyFileVersion("0.9.2.3")] +[assembly: AssemblyVersion("0.9.2.4")] +[assembly: AssemblyFileVersion("0.9.2.4")] diff --git a/SystemTrayMenu.cs b/SystemTrayMenu.cs index b9c416c..7b7d9f3 100644 --- a/SystemTrayMenu.cs +++ b/SystemTrayMenu.cs @@ -1,5 +1,4 @@ using Clearcove.Logging; -using Shell32; using System; using System.Collections.Generic; using System.ComponentModel; @@ -14,16 +13,8 @@ using SystemTrayMenu.Helper; namespace SystemTrayMenu { - #region Enable debug log by putting this code into each function - //MethodBase m = MethodBase.GetCurrentMethod(); - //log.Debug($"Executing {m.ReflectedType.Name}, {m.Name}"); - #endregion class SystemTrayMenu : IDisposable { - Logger log = new Logger(nameof(SystemTrayMenu)); - - IShellDispatch4 iShellDispatch4 = null; - MessageFilter messageFilter = new MessageFilter(); bool messageFilterAdded = false; @@ -43,18 +34,6 @@ namespace SystemTrayMenu public SystemTrayMenu() { - Log.ApplicationStart(); - - try - { - iShellDispatch4 = (IShellDispatch4)Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application")); - } - catch (Exception ex) - { - log.Error("Get Shell COM instance failed" + Environment.NewLine + ex.ToString()); - } - - Config.UpgradeIfNotUpgraded(); keyboardInput = new KeyboardInput(menus); keyboardInput.RegisterHotKey(); keyboardInput.HotKeyPressed += SwitchOpenClose; @@ -188,7 +167,7 @@ namespace SystemTrayMenu Microsoft.Win32.SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged; void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e) { - log.Info("SystemEvents_DisplaySettingsChanged"); + Log.Info("SystemEvents_DisplaySettingsChanged"); ApplicationRestart(); } @@ -392,21 +371,6 @@ namespace SystemTrayMenu MenuData ReadMenu(BackgroundWorker worker, string path, int level) { - bool HideHiddenEntries = false; - bool HideSystemEntries = false; - - if (null != iShellDispatch4) - { - // Using SHGetSetSettings would be much better in performance but the results are not accurate. - // We have to go for the shell interface in order to receive the correct settings: - // https://docs.microsoft.com/en-us/windows/win32/shell/ishelldispatch4-getsetting - const int SSF_SHOWALLOBJECTS = 0x00000001; - const int SSF_SHOWSUPERHIDDEN = 0x00040000; - - HideHiddenEntries = !iShellDispatch4.GetSetting(SSF_SHOWALLOBJECTS); - HideSystemEntries = !iShellDispatch4.GetSetting(SSF_SHOWSUPERHIDDEN); - } - MenuData menuData = new MenuData(); menuData.RowDatas = new List(); menuData.Validity = MenuDataValidity.Invalid; @@ -422,12 +386,11 @@ namespace SystemTrayMenu } catch (UnauthorizedAccessException) { - log.Info($"UnauthorizedAccessException:'{path}'"); + Log.Info($"UnauthorizedAccessException:'{path}'"); } catch (Exception ex) { - log.Info($"path:'{path}'"); - log.Error($"{ex.ToString()}"); + Log.Error($"path:'{path}'", ex); } foreach (string directory in directories) @@ -437,12 +400,8 @@ namespace SystemTrayMenu break; } - FileAttributes attributes = File.GetAttributes(directory); - bool hiddenEntry = attributes.HasFlag(FileAttributes.Hidden); - bool systemEntry = attributes.HasFlag( - FileAttributes.Hidden | FileAttributes.System); - if ((HideHiddenEntries && hiddenEntry) || - (HideSystemEntries && systemEntry)) + bool hiddenEntry = false; + if (FolderOptions.IsHidden(directory, ref hiddenEntry)) { continue; } @@ -468,15 +427,14 @@ namespace SystemTrayMenu ).ToArray(); Array.Sort(files, new WindowsExplorerSort()); } + catch (UnauthorizedAccessException) + { + Log.Info($"UnauthorizedAccessException:'{path}'"); + menuData.Validity = MenuDataValidity.NoAccess; + } catch (Exception ex) { - if ((uint)ex.HResult == 0x80070005) // E_ACCESSDENIED - menuData.Validity = MenuDataValidity.NoAccess; - else - { - log.Info($"path:'{path}'"); - log.Error($"{ex.ToString()}"); - } + Log.Error($"path:'{path}'", ex); } foreach (string file in files) @@ -486,12 +444,8 @@ namespace SystemTrayMenu break; } - FileAttributes attributes = File.GetAttributes(file); - bool hiddenEntry = attributes.HasFlag(FileAttributes.Hidden); - bool systemEntry = attributes.HasFlag( - FileAttributes.Hidden | FileAttributes.System); - if ((HideHiddenEntries && hiddenEntry) || - (HideSystemEntries && systemEntry)) + bool hiddenEntry = false; + if (FolderOptions.IsHidden(file, ref hiddenEntry)) { continue; } @@ -500,7 +454,6 @@ namespace SystemTrayMenu string resolvedLnkPath = string.Empty; if (menuButtonData.ReadIcon(false, ref resolvedLnkPath)) { - // file is pointing to a directory, so prepare submenu menuButtonData = ReadMenuButtonData(resolvedLnkPath, true, menuButtonData); menuButtonData.ContainsMenu = true; menuButtonData.HiddenEntry = hiddenEntry; @@ -540,8 +493,7 @@ namespace SystemTrayMenu } catch (Exception ex) { - log.Info($"fileName:'{fileName}'"); - log.Error($"{ex.ToString()}"); + Log.Error($"fileName:'{fileName}'", ex); } return menuButtonData; diff --git a/SystemTrayMenu.csproj b/SystemTrayMenu.csproj index c40f650..9a7288c 100644 --- a/SystemTrayMenu.csproj +++ b/SystemTrayMenu.csproj @@ -164,6 +164,7 @@ +