mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-08 20:29:51 +01:00
Compare commits
1 Commits
issue/3930
...
issue/2802
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d4c858288f |
80
src/modules/peek/Peek.Common/Helpers/SymlinkResolver.cs
Normal file
80
src/modules/peek/Peek.Common/Helpers/SymlinkResolver.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
// PeekSymlinkResolver.cs
|
||||
// Fix for Issue #28028: Peek can't view PDF/HTML soft links
|
||||
// This helper resolves symbolic links to their target paths
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Peek.Common.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Resolves symbolic links and junction points to their target paths.
|
||||
/// </summary>
|
||||
public static class SymlinkResolver
|
||||
{
|
||||
/// <summary>
|
||||
/// Resolves a path to its final target if it's a symbolic link or junction.
|
||||
/// </summary>
|
||||
/// <param name="path">The path to resolve.</param>
|
||||
/// <returns>The resolved target path, or the original path if not a link.</returns>
|
||||
public static string ResolveSymlink(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var fileInfo = new FileInfo(path);
|
||||
|
||||
// Check if it's a symbolic link
|
||||
if (fileInfo.Attributes.HasFlag(FileAttributes.ReparsePoint))
|
||||
{
|
||||
// Get the target of the symbolic link
|
||||
var target = fileInfo.LinkTarget;
|
||||
if (!string.IsNullOrEmpty(target))
|
||||
{
|
||||
// If target is relative, make it absolute
|
||||
if (!Path.IsPathRooted(target))
|
||||
{
|
||||
var directory = Path.GetDirectoryName(path);
|
||||
target = Path.GetFullPath(Path.Combine(directory ?? string.Empty, target));
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// If resolution fails, return the original path
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a path is a symbolic link or junction point.
|
||||
/// </summary>
|
||||
public static bool IsSymlink(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path) || !File.Exists(path))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var attributes = File.GetAttributes(path);
|
||||
return attributes.HasFlag(FileAttributes.ReparsePoint);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
// WorkingDirectoryHelper.cs
|
||||
// Fix for Issue #39305: Peek's "open with" leaves working directory in PowerToys
|
||||
// Ensures launched applications get the correct working directory
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
|
||||
namespace Peek.Common.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper for launching external applications with correct working directory.
|
||||
/// </summary>
|
||||
public static class WorkingDirectoryHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Launches an application with the working directory set to the file's location.
|
||||
/// </summary>
|
||||
/// <param name="filePath">The file to open.</param>
|
||||
/// <param name="applicationPath">Optional specific application to use.</param>
|
||||
public static void OpenWithCorrectWorkingDirectory(string filePath, string? applicationPath = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var directory = Path.GetDirectoryName(filePath);
|
||||
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = applicationPath ?? filePath,
|
||||
WorkingDirectory = directory ?? Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
|
||||
UseShellExecute = true,
|
||||
};
|
||||
|
||||
if (!string.IsNullOrEmpty(applicationPath))
|
||||
{
|
||||
startInfo.Arguments = $"\"{filePath}\"";
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Process.Start(startInfo);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Log error but don't throw - graceful degradation
|
||||
System.Diagnostics.Debug.WriteLine($"Failed to open file: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens the "Open with" dialog with correct working directory.
|
||||
/// </summary>
|
||||
public static void ShowOpenWithDialog(string filePath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var directory = Path.GetDirectoryName(filePath);
|
||||
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "rundll32.exe",
|
||||
Arguments = $"shell32.dll,OpenAs_RunDLL \"{filePath}\"",
|
||||
WorkingDirectory = directory ?? string.Empty,
|
||||
UseShellExecute = false,
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
Process.Start(startInfo);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Fallback to default behavior
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user