[Feature] CodeBuity&Refactor #49

This commit is contained in:
Markus Hofknecht 2020-03-16 15:57:51 +01:00
parent 5a30e8c25c
commit 62cc2a7d6d
7 changed files with 80 additions and 70 deletions

View file

@ -116,7 +116,7 @@ namespace SystemTrayMenu.Controls
else if (string.IsNullOrEmpty(resolvedLnkPath)) else if (string.IsNullOrEmpty(resolvedLnkPath))
{ {
ResolvedFileNotFound = true; 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 #warning [Feature] Resolve network root #48, start here
} }
else else
@ -181,7 +181,7 @@ namespace SystemTrayMenu.Controls
} }
else else
{ {
Log.Info($"Resolve *.URL '{TargetFilePath}' not possible => no icon"); Log.Info($"Resolve *.URL '{TargetFilePath}' has no icon");
} }
} }
catch (Exception ex) catch (Exception ex)

54
Helper/FolderOptions.cs Normal file
View file

@ -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;
}
}
}

View file

@ -42,13 +42,13 @@ namespace SystemTrayMenu.Helper
Process.Start(GetLogFilePath()); Process.Start(GetLogFilePath());
} }
internal static void ApplicationStart() internal static void ApplicationRun()
{ {
Assembly assembly = Assembly.GetExecutingAssembly(); Assembly assembly = Assembly.GetExecutingAssembly();
log.Info($"Application Start " + log.Info($"Application Start " +
assembly.ManifestModule.FullyQualifiedName + "|" + assembly.ManifestModule.Name + " | " +
assembly.GetName().Version.ToString() + "|" + assembly.GetName().Version.ToString() + " | " +
$" ScalingFactor={Scaling.Factor}"); $"ScalingFactor={Scaling.Factor}");
} }
} }
} }

View file

@ -19,6 +19,7 @@ namespace SystemTrayMenu
SingleAppInstance.Initialize(); SingleAppInstance.Initialize();
Language.Initialize(); Language.Initialize();
Config.UpgradeIfNotUpgraded();
if (Config.LoadOrSetByUser()) if (Config.LoadOrSetByUser())
{ {
Application.EnableVisualStyles(); Application.EnableVisualStyles();
@ -30,9 +31,11 @@ namespace SystemTrayMenu
} }
Scaling.Initialize(); Scaling.Initialize();
FolderOptions.Initialize();
using (new SystemTrayMenu()) using (new SystemTrayMenu())
{ {
Log.ApplicationRun();
Application.Run(); Application.Run();
} }
} }

View file

@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.9.2.3")] [assembly: AssemblyVersion("0.9.2.4")]
[assembly: AssemblyFileVersion("0.9.2.3")] [assembly: AssemblyFileVersion("0.9.2.4")]

View file

@ -1,5 +1,4 @@
using Clearcove.Logging; using Clearcove.Logging;
using Shell32;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@ -14,16 +13,8 @@ using SystemTrayMenu.Helper;
namespace SystemTrayMenu 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 class SystemTrayMenu : IDisposable
{ {
Logger log = new Logger(nameof(SystemTrayMenu));
IShellDispatch4 iShellDispatch4 = null;
MessageFilter messageFilter = new MessageFilter(); MessageFilter messageFilter = new MessageFilter();
bool messageFilterAdded = false; bool messageFilterAdded = false;
@ -43,18 +34,6 @@ namespace SystemTrayMenu
public 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 = new KeyboardInput(menus);
keyboardInput.RegisterHotKey(); keyboardInput.RegisterHotKey();
keyboardInput.HotKeyPressed += SwitchOpenClose; keyboardInput.HotKeyPressed += SwitchOpenClose;
@ -188,7 +167,7 @@ namespace SystemTrayMenu
Microsoft.Win32.SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged; Microsoft.Win32.SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e) void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{ {
log.Info("SystemEvents_DisplaySettingsChanged"); Log.Info("SystemEvents_DisplaySettingsChanged");
ApplicationRestart(); ApplicationRestart();
} }
@ -392,21 +371,6 @@ namespace SystemTrayMenu
MenuData ReadMenu(BackgroundWorker worker, string path, int level) 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 menuData = new MenuData();
menuData.RowDatas = new List<RowData>(); menuData.RowDatas = new List<RowData>();
menuData.Validity = MenuDataValidity.Invalid; menuData.Validity = MenuDataValidity.Invalid;
@ -422,12 +386,11 @@ namespace SystemTrayMenu
} }
catch (UnauthorizedAccessException) catch (UnauthorizedAccessException)
{ {
log.Info($"UnauthorizedAccessException:'{path}'"); Log.Info($"UnauthorizedAccessException:'{path}'");
} }
catch (Exception ex) catch (Exception ex)
{ {
log.Info($"path:'{path}'"); Log.Error($"path:'{path}'", ex);
log.Error($"{ex.ToString()}");
} }
foreach (string directory in directories) foreach (string directory in directories)
@ -437,12 +400,8 @@ namespace SystemTrayMenu
break; break;
} }
FileAttributes attributes = File.GetAttributes(directory); bool hiddenEntry = false;
bool hiddenEntry = attributes.HasFlag(FileAttributes.Hidden); if (FolderOptions.IsHidden(directory, ref hiddenEntry))
bool systemEntry = attributes.HasFlag(
FileAttributes.Hidden | FileAttributes.System);
if ((HideHiddenEntries && hiddenEntry) ||
(HideSystemEntries && systemEntry))
{ {
continue; continue;
} }
@ -468,15 +427,14 @@ namespace SystemTrayMenu
).ToArray(); ).ToArray();
Array.Sort(files, new WindowsExplorerSort()); Array.Sort(files, new WindowsExplorerSort());
} }
catch (UnauthorizedAccessException)
{
Log.Info($"UnauthorizedAccessException:'{path}'");
menuData.Validity = MenuDataValidity.NoAccess;
}
catch (Exception ex) catch (Exception ex)
{ {
if ((uint)ex.HResult == 0x80070005) // E_ACCESSDENIED Log.Error($"path:'{path}'", ex);
menuData.Validity = MenuDataValidity.NoAccess;
else
{
log.Info($"path:'{path}'");
log.Error($"{ex.ToString()}");
}
} }
foreach (string file in files) foreach (string file in files)
@ -486,12 +444,8 @@ namespace SystemTrayMenu
break; break;
} }
FileAttributes attributes = File.GetAttributes(file); bool hiddenEntry = false;
bool hiddenEntry = attributes.HasFlag(FileAttributes.Hidden); if (FolderOptions.IsHidden(file, ref hiddenEntry))
bool systemEntry = attributes.HasFlag(
FileAttributes.Hidden | FileAttributes.System);
if ((HideHiddenEntries && hiddenEntry) ||
(HideSystemEntries && systemEntry))
{ {
continue; continue;
} }
@ -500,7 +454,6 @@ namespace SystemTrayMenu
string resolvedLnkPath = string.Empty; string resolvedLnkPath = string.Empty;
if (menuButtonData.ReadIcon(false, ref resolvedLnkPath)) if (menuButtonData.ReadIcon(false, ref resolvedLnkPath))
{ {
// file is pointing to a directory, so prepare submenu
menuButtonData = ReadMenuButtonData(resolvedLnkPath, true, menuButtonData); menuButtonData = ReadMenuButtonData(resolvedLnkPath, true, menuButtonData);
menuButtonData.ContainsMenu = true; menuButtonData.ContainsMenu = true;
menuButtonData.HiddenEntry = hiddenEntry; menuButtonData.HiddenEntry = hiddenEntry;
@ -540,8 +493,7 @@ namespace SystemTrayMenu
} }
catch (Exception ex) catch (Exception ex)
{ {
log.Info($"fileName:'{fileName}'"); Log.Error($"fileName:'{fileName}'", ex);
log.Error($"{ex.ToString()}");
} }
return menuButtonData; return menuButtonData;

View file

@ -164,6 +164,7 @@
<Compile Include="Helper\File\IconReader.cs" /> <Compile Include="Helper\File\IconReader.cs" />
<Compile Include="Helper\File\FileIni.cs" /> <Compile Include="Helper\File\FileIni.cs" />
<Compile Include="Helper\File\FileLnk.cs" /> <Compile Include="Helper\File\FileLnk.cs" />
<Compile Include="Helper\FolderOptions.cs" />
<Compile Include="Helper\KeyboardHook.cs" /> <Compile Include="Helper\KeyboardHook.cs" />
<Compile Include="Helper\Log.cs" /> <Compile Include="Helper\Log.cs" />
<Compile Include="Helper\SingleAppInstance.cs" /> <Compile Include="Helper\SingleAppInstance.cs" />