SystemTrayMenu/Utilities/File/FileLnk.cs

125 lines
3.9 KiB
C#
Raw Normal View History

// <copyright file="FileLnk.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu.Utilities
2019-07-05 05:04:14 +12:00
{
using System;
using System.IO;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Threading;
using Shell32;
internal class FileLnk
2019-07-05 05:04:14 +12:00
{
2020-04-30 23:22:47 +12:00
public static string GetResolvedFileName(string shortcutFilename)
2019-07-05 05:04:14 +12:00
{
2020-04-30 23:22:47 +12:00
string resolvedFilename = string.Empty;
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
{
resolvedFilename = GetShortcutFileNamePath(shortcutFilename);
}
else
{
Thread staThread = new Thread(new ParameterizedThreadStart(StaThreadMethod));
void StaThreadMethod(object obj)
{
resolvedFilename = GetShortcutFileNamePath(shortcutFilename);
}
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
2020-04-30 23:22:47 +12:00
return resolvedFilename;
2019-07-05 05:04:14 +12:00
}
public static bool IsDirectory(string filePath)
{
bool isDirectory = false;
if (Directory.Exists(filePath))
{
FileAttributes attributes = File.GetAttributes(filePath);
if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
isDirectory = true;
}
}
return isDirectory;
}
public static bool IsNetworkRoot(string path)
{
return !File.Exists(path) &&
path.StartsWith(@"\\", StringComparison.InvariantCulture) &&
!path.Substring(2).Contains(@"\", StringComparison.InvariantCulture);
}
public static bool IsNetworkPath(string path)
{
return path.StartsWith(@"\\", StringComparison.InvariantCulture) &&
!path.StartsWith(@"\\?\", StringComparison.InvariantCulture);
}
public static bool PingHost(string nameOrAddress)
{
bool pingable = false;
Ping pinger = null;
try
{
pinger = new Ping();
PingReply reply = pinger.Send(nameOrAddress);
pingable = reply.Status == IPStatus.Success;
}
catch (PingException ex)
{
Log.Warn($"Ping {nameOrAddress} failed", ex);
}
finally
{
if (pinger != null)
{
pinger.Dispose();
}
}
return pingable;
}
2020-04-30 23:22:47 +12:00
private static string GetShortcutFileNamePath(object shortcutFilename)
2019-07-05 05:04:14 +12:00
{
2020-04-30 23:22:47 +12:00
string resolvedFilename = string.Empty;
string pathOnly = Path.GetDirectoryName((string)shortcutFilename);
string filenameOnly = Path.GetFileName((string)shortcutFilename);
2019-07-05 05:04:14 +12:00
2020-04-30 23:22:47 +12:00
Shell shell = new Shell();
Folder folder = shell.NameSpace(pathOnly);
FolderItem folderItem = folder.ParseName(filenameOnly);
if (folderItem != null)
{
2020-05-09 01:57:30 +12:00
try
2020-04-30 23:22:47 +12:00
{
2020-05-09 01:57:30 +12:00
ShellLinkObject link = (ShellLinkObject)folderItem.GetLink;
if (string.IsNullOrEmpty(link.Path))
{
resolvedFilename = link.Target.Path;
}
else
{
resolvedFilename = link.Path;
}
2020-04-30 23:22:47 +12:00
}
catch (Exception ex)
2020-04-30 23:22:47 +12:00
{
Log.Warn($"shortcutFilename:'{shortcutFilename}'", ex);
2020-04-30 23:22:47 +12:00
}
}
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
}
}
}