mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Add error handling and validation for custom file manager paths
This commit is contained in:
parent
037b800f4a
commit
2c7fb93d37
3 changed files with 114 additions and 31 deletions
|
|
@ -373,6 +373,8 @@
|
|||
<system:String x:Key="fileManager_path">File Manager Path</system:String>
|
||||
<system:String x:Key="fileManager_directory_arg">Arg For Folder</system:String>
|
||||
<system:String x:Key="fileManager_file_arg">Arg For File</system:String>
|
||||
<system:String x:Key="fileManagerPathNotFound">The path for the file manager '{0}' could not be found: {1}\n\nDo you want to continue?</system:String>
|
||||
<system:String x:Key="fileManagerPathError">File Manager Path Error</system:String>
|
||||
|
||||
<!-- DefaultBrowser Setting Dialog -->
|
||||
<system:String x:Key="defaultBrowserTitle">Default Web Browser</system:String>
|
||||
|
|
@ -462,6 +464,10 @@
|
|||
<system:String x:Key="reportWindow_upload_log">1. Upload log file: {0}</system:String>
|
||||
<system:String x:Key="reportWindow_copy_below">2. Copy below exception message</system:String>
|
||||
|
||||
<!-- File Open Error -->
|
||||
<system:String x:Key="folderOpenError">An error occurred while opening the folder.:\n{0}</system:String>
|
||||
<system:String x:Key="errorTitle">Error</system:String>
|
||||
|
||||
<!-- General Notice -->
|
||||
<system:String x:Key="pleaseWait">Please wait...</system:String>
|
||||
|
||||
|
|
|
|||
|
|
@ -316,40 +316,63 @@ namespace Flow.Launcher
|
|||
|
||||
public void OpenDirectory(string DirectoryPath, string FileNameOrFilePath = null)
|
||||
{
|
||||
using var explorer = new Process();
|
||||
var explorerInfo = _settings.CustomExplorer;
|
||||
var explorerPath = explorerInfo.Path.Trim().ToLowerInvariant();
|
||||
var targetPath = FileNameOrFilePath is null
|
||||
? DirectoryPath
|
||||
: Path.IsPathRooted(FileNameOrFilePath)
|
||||
? FileNameOrFilePath
|
||||
: Path.Combine(DirectoryPath, FileNameOrFilePath);
|
||||
try
|
||||
{
|
||||
using var explorer = new Process();
|
||||
var explorerInfo = _settings.CustomExplorer;
|
||||
var explorerPath = explorerInfo.Path.Trim().ToLowerInvariant();
|
||||
var targetPath = FileNameOrFilePath is null
|
||||
? DirectoryPath
|
||||
: Path.IsPathRooted(FileNameOrFilePath)
|
||||
? FileNameOrFilePath
|
||||
: Path.Combine(DirectoryPath, FileNameOrFilePath);
|
||||
|
||||
if (Path.GetFileNameWithoutExtension(explorerPath) == "explorer")
|
||||
{
|
||||
// Windows File Manager
|
||||
// We should ignore and pass only the path to Shell to prevent zombie explorer.exe processes
|
||||
explorer.StartInfo = new ProcessStartInfo
|
||||
if (Path.GetFileNameWithoutExtension(explorerPath) == "explorer")
|
||||
{
|
||||
FileName = targetPath, // Not explorer, Only path.
|
||||
UseShellExecute = true // Must be true to open folder
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
// Custom File Manager
|
||||
explorer.StartInfo = new ProcessStartInfo
|
||||
// Windows File Manager
|
||||
explorer.StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = targetPath,
|
||||
UseShellExecute = true
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
FileName = explorerInfo.Path.Replace("%d", DirectoryPath),
|
||||
UseShellExecute = true,
|
||||
Arguments = FileNameOrFilePath is null
|
||||
? explorerInfo.DirectoryArgument.Replace("%d", DirectoryPath)
|
||||
: explorerInfo.FileArgument
|
||||
.Replace("%d", DirectoryPath)
|
||||
.Replace("%f", targetPath)
|
||||
};
|
||||
// Custom File Manager
|
||||
explorer.StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = explorerInfo.Path.Replace("%d", DirectoryPath),
|
||||
UseShellExecute = true,
|
||||
Arguments = FileNameOrFilePath is null
|
||||
? explorerInfo.DirectoryArgument.Replace("%d", DirectoryPath)
|
||||
: explorerInfo.FileArgument
|
||||
.Replace("%d", DirectoryPath)
|
||||
.Replace("%f", targetPath)
|
||||
};
|
||||
}
|
||||
|
||||
explorer.Start();
|
||||
}
|
||||
catch (System.ComponentModel.Win32Exception ex) when (ex.NativeErrorCode == 2)
|
||||
{
|
||||
// File Manager not found
|
||||
MessageBoxEx.Show(
|
||||
string.Format(GetTranslation("fileManagerNotFound"), ex.Message),
|
||||
GetTranslation("fileManagerNotFoundTitle"),
|
||||
MessageBoxButton.OK,
|
||||
MessageBoxImage.Error
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Other exceptions
|
||||
MessageBoxEx.Show(
|
||||
string.Format(GetTranslation("folderOpenError"), ex.Message),
|
||||
GetTranslation("errorTitle"),
|
||||
MessageBoxButton.OK,
|
||||
MessageBoxImage.Error
|
||||
);
|
||||
}
|
||||
explorer.Start();
|
||||
}
|
||||
|
||||
private void OpenUri(Uri uri, bool? inPrivate = null)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
using System.Collections.ObjectModel;
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
|
|
@ -43,11 +46,62 @@ namespace Flow.Launcher
|
|||
|
||||
private void btnDone_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// Check if the selected file manager path is valid
|
||||
if (!IsFileManagerValid(CustomExplorer.Path))
|
||||
{
|
||||
MessageBoxResult result = MessageBoxEx.Show(
|
||||
string.Format((string)Application.Current.FindResource("fileManagerPathNotFound"),
|
||||
CustomExplorer.Name, CustomExplorer.Path),
|
||||
(string)Application.Current.FindResource("fileManagerPathError"),
|
||||
MessageBoxButton.YesNo,
|
||||
MessageBoxImage.Warning);
|
||||
|
||||
if (result == MessageBoxResult.No)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_settings.CustomExplorerList = CustomExplorers.ToList();
|
||||
_settings.CustomExplorerIndex = SelectedCustomExplorerIndex;
|
||||
Close();
|
||||
}
|
||||
|
||||
private bool IsFileManagerValid(string path)
|
||||
{
|
||||
if (string.Equals(path, "explorer", StringComparison.OrdinalIgnoreCase))
|
||||
return true;
|
||||
|
||||
if (Path.IsPathRooted(path))
|
||||
{
|
||||
return File.Exists(path);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var process = new Process
|
||||
{
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "where",
|
||||
Arguments = path,
|
||||
RedirectStandardOutput = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true
|
||||
}
|
||||
};
|
||||
process.Start();
|
||||
string output = process.StandardOutput.ReadToEnd();
|
||||
process.WaitForExit();
|
||||
|
||||
return !string.IsNullOrEmpty(output);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void btnAdd_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
CustomExplorers.Add(new()
|
||||
|
|
|
|||
Loading…
Reference in a new issue