2020-07-07 09:37:55 +12:00
|
|
|
|
// <copyright file="FileLnk.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
|
|
|
|
|
namespace SystemTrayMenu.Utilities
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading;
|
2020-07-08 03:05:19 +12:00
|
|
|
|
using Shell32;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
|
|
|
|
internal class FileLnk
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2021-11-23 09:49:36 +13:00
|
|
|
|
public static string GetResolvedFileName(string shortcutFilename, out bool isFolder)
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2021-11-23 09:49:36 +13:00
|
|
|
|
bool isFolderByShell = false;
|
2020-04-30 23:22:47 +12:00
|
|
|
|
string resolvedFilename = string.Empty;
|
|
|
|
|
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
|
|
|
|
|
{
|
2021-11-23 09:49:36 +13:00
|
|
|
|
resolvedFilename = GetShortcutFileNamePath(shortcutFilename, out isFolderByShell);
|
2020-04-30 23:22:47 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-11-17 12:13:46 +13:00
|
|
|
|
Thread staThread = new(new ParameterizedThreadStart(StaThreadMethod));
|
2020-04-30 23:22:47 +12:00
|
|
|
|
void StaThreadMethod(object obj)
|
|
|
|
|
{
|
2021-11-23 09:49:36 +13:00
|
|
|
|
resolvedFilename = GetShortcutFileNamePath(shortcutFilename, out isFolderByShell);
|
2020-04-30 23:22:47 +12:00
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-04-30 23:22:47 +12:00
|
|
|
|
staThread.SetApartmentState(ApartmentState.STA);
|
|
|
|
|
staThread.Start(shortcutFilename);
|
|
|
|
|
staThread.Join();
|
|
|
|
|
}
|
2019-07-05 05:04:14 +12:00
|
|
|
|
|
2021-11-23 09:49:36 +13:00
|
|
|
|
isFolder = isFolderByShell;
|
2020-04-30 23:22:47 +12:00
|
|
|
|
return resolvedFilename;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 09:47:47 +13:00
|
|
|
|
public static bool IsNetworkRoot(string path)
|
2021-05-03 01:01:31 +12:00
|
|
|
|
{
|
|
|
|
|
return path.StartsWith(@"\\", StringComparison.InvariantCulture) &&
|
2022-02-07 04:12:46 +13:00
|
|
|
|
!path[2..].Contains('\\', StringComparison.InvariantCulture);
|
2021-05-03 01:01:31 +12:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 09:49:36 +13:00
|
|
|
|
private static string GetShortcutFileNamePath(object shortcutFilename, out bool isFolder)
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2020-04-30 23:22:47 +12:00
|
|
|
|
string resolvedFilename = string.Empty;
|
2021-11-23 09:49:36 +13:00
|
|
|
|
isFolder = false;
|
2022-10-25 06:31:05 +13:00
|
|
|
|
try
|
2020-04-30 23:22:47 +12:00
|
|
|
|
{
|
2022-10-25 06:31:05 +13:00
|
|
|
|
string pathOnly = Path.GetDirectoryName((string)shortcutFilename);
|
|
|
|
|
string filenameOnly = Path.GetFileName((string)shortcutFilename);
|
|
|
|
|
|
|
|
|
|
Shell shell = new();
|
|
|
|
|
Folder folder = shell.NameSpace(pathOnly);
|
|
|
|
|
if (folder == null)
|
2020-04-30 23:22:47 +12:00
|
|
|
|
{
|
2022-10-25 06:31:05 +13:00
|
|
|
|
Log.Info($"{nameof(GetShortcutFileNamePath)} folder == null for path:'{shortcutFilename}'");
|
|
|
|
|
return resolvedFilename;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FolderItem folderItem = folder.ParseName(filenameOnly);
|
|
|
|
|
if (folderItem == null)
|
|
|
|
|
{
|
|
|
|
|
Log.Info($"{nameof(GetShortcutFileNamePath)} folderItem == null for path:'{shortcutFilename}'");
|
|
|
|
|
return resolvedFilename;
|
2020-04-30 23:22:47 +12:00
|
|
|
|
}
|
2022-10-25 06:31:05 +13:00
|
|
|
|
|
|
|
|
|
ShellLinkObject link = (ShellLinkObject)folderItem.GetLink;
|
|
|
|
|
isFolder = link.Target.IsFolder;
|
|
|
|
|
if (string.IsNullOrEmpty(link.Path))
|
2022-02-21 06:43:57 +13:00
|
|
|
|
{
|
2022-10-25 06:31:05 +13:00
|
|
|
|
// https://github.com/Hofknecht/SystemTrayMenu/issues/242
|
|
|
|
|
// do not set CLSID key (GUID) shortcuts as resolvedFilename
|
|
|
|
|
if (!link.Target.Path.Contains("::{"))
|
|
|
|
|
{
|
|
|
|
|
resolvedFilename = link.Target.Path;
|
|
|
|
|
}
|
2022-02-21 06:43:57 +13:00
|
|
|
|
}
|
2022-10-25 06:31:05 +13:00
|
|
|
|
else
|
2020-04-30 23:22:47 +12:00
|
|
|
|
{
|
2022-10-25 06:31:05 +13:00
|
|
|
|
resolvedFilename = link.Path;
|
2020-04-30 23:22:47 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-25 06:31:05 +13:00
|
|
|
|
catch (UnauthorizedAccessException)
|
|
|
|
|
{
|
|
|
|
|
// https://stackoverflow.com/questions/2934420/why-do-i-get-e-accessdenied-when-reading-public-shortcuts-through-shell32
|
|
|
|
|
// e.g. Administrative Tools\Component Services.lnk which can not be resolved, do not spam the logfile in this case
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn($"shortcutFilename:'{shortcutFilename}'", ex);
|
|
|
|
|
}
|
2019-07-05 05:04:14 +12:00
|
|
|
|
|
2020-04-30 23:22:47 +12:00
|
|
|
|
return resolvedFilename;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|