50 lines
1.3 KiB
C#
50 lines
1.3 KiB
C#
|
|
using System.Diagnostics;
|
||
|
|
using System.IO;
|
||
|
|
|
||
|
|
namespace ImageCatalog_2.Services;
|
||
|
|
|
||
|
|
public static class PathShellService
|
||
|
|
{
|
||
|
|
public static void OpenInExplorer(string? path)
|
||
|
|
{
|
||
|
|
if (string.IsNullOrWhiteSpace(path))
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
var normalizedPath = path.Trim().Trim('"');
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
if (File.Exists(normalizedPath))
|
||
|
|
{
|
||
|
|
Process.Start("explorer.exe", $"/select,\"{normalizedPath}\"");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (Directory.Exists(normalizedPath))
|
||
|
|
{
|
||
|
|
Process.Start(new ProcessStartInfo
|
||
|
|
{
|
||
|
|
FileName = normalizedPath,
|
||
|
|
UseShellExecute = true
|
||
|
|
});
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
var containingDirectory = Path.GetDirectoryName(normalizedPath);
|
||
|
|
if (!string.IsNullOrWhiteSpace(containingDirectory) && Directory.Exists(containingDirectory))
|
||
|
|
{
|
||
|
|
Process.Start(new ProcessStartInfo
|
||
|
|
{
|
||
|
|
FileName = containingDirectory,
|
||
|
|
UseShellExecute = true
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch
|
||
|
|
{
|
||
|
|
// Ignore failures when opening Explorer.
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|