2020-07-07 07:15:45 +12:00
|
|
|
|
// <copyright file="FolderOptions.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
2020-03-17 03:57:51 +13:00
|
|
|
|
|
2020-03-28 12:42:32 +13:00
|
|
|
|
namespace SystemTrayMenu.Utilities
|
2020-03-17 03:57:51 +13:00
|
|
|
|
{
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2020-07-08 03:05:19 +12:00
|
|
|
|
using Shell32;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2020-03-17 03:57:51 +13:00
|
|
|
|
internal static class FolderOptions
|
|
|
|
|
{
|
2021-04-17 12:39:48 +12:00
|
|
|
|
private static bool hideHiddenEntries;
|
|
|
|
|
private static bool hideSystemEntries;
|
|
|
|
|
private static IShellDispatch4 iShellDispatch4;
|
2020-03-17 03:57:51 +13:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2020-03-30 06:26:45 +13:00
|
|
|
|
if (ex is ArgumentException ||
|
|
|
|
|
ex is NotSupportedException ||
|
|
|
|
|
ex is TargetInvocationException ||
|
|
|
|
|
ex is MethodAccessException ||
|
|
|
|
|
ex is MemberAccessException ||
|
|
|
|
|
ex is InvalidComObjectException ||
|
|
|
|
|
ex is MissingMethodException ||
|
|
|
|
|
ex is COMException ||
|
|
|
|
|
ex is TypeLoadException)
|
|
|
|
|
{
|
|
|
|
|
#pragma warning disable CA1303 // Do not pass literals as localized parameters
|
|
|
|
|
Log.Warn("Get Shell COM instance failed", ex);
|
|
|
|
|
#pragma warning restore CA1303 //=> Exceptions not translated in logfile => OK
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2020-03-17 03:57:51 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static bool IsHidden(string path, ref bool hiddenEntry)
|
|
|
|
|
{
|
|
|
|
|
bool isDirectoryToHide = false;
|
2020-04-12 04:14:23 +12:00
|
|
|
|
if (path.Length < 260)
|
2020-03-17 03:57:51 +13:00
|
|
|
|
{
|
2020-09-27 01:54:24 +12:00
|
|
|
|
try
|
2020-04-12 04:14:23 +12:00
|
|
|
|
{
|
2020-09-27 01:54:24 +12:00
|
|
|
|
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;
|
|
|
|
|
}
|
2020-04-12 04:14:23 +12:00
|
|
|
|
}
|
2020-10-25 00:12:59 +13:00
|
|
|
|
catch (Exception ex)
|
2020-09-27 01:54:24 +12:00
|
|
|
|
{
|
2021-09-30 09:21:02 +13:00
|
|
|
|
Log.Warn($"path:'{path}'", ex);
|
2020-09-27 01:54:24 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Log.Info($"path too long (>=260):'{path}'");
|
2020-03-17 03:57:51 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isDirectoryToHide;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|