fix a crashing bug

System.ArgumentException thrown by Path.GetFullPath
This commit is contained in:
Genteure 2020-12-17 21:46:27 +08:00
parent 914b9958cd
commit b787fbda02

View File

@ -15,6 +15,7 @@ using BililiveRecorder.WPF.Models;
using CommandLine; using CommandLine;
using ModernWpf.Controls; using ModernWpf.Controls;
using ModernWpf.Media.Animation; using ModernWpf.Media.Animation;
using NLog;
using Path = System.IO.Path; using Path = System.IO.Path;
namespace BililiveRecorder.WPF.Pages namespace BililiveRecorder.WPF.Pages
@ -24,6 +25,8 @@ namespace BililiveRecorder.WPF.Pages
/// </summary> /// </summary>
public partial class RootPage : UserControl public partial class RootPage : UserControl
{ {
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
private readonly Dictionary<string, Type> PageMap = new Dictionary<string, Type>(); private readonly Dictionary<string, Type> PageMap = new Dictionary<string, Type>();
private readonly string lastdir_path = Path.Combine(Path.GetDirectoryName(typeof(RootPage).Assembly.Location), "lastdir.txt"); private readonly string lastdir_path = Path.Combine(Path.GetDirectoryName(typeof(RootPage).Assembly.Location), "lastdir.txt");
private readonly NavigationTransitionInfo transitionInfo = new DrillInNavigationTransitionInfo(); private readonly NavigationTransitionInfo transitionInfo = new DrillInNavigationTransitionInfo();
@ -68,15 +71,19 @@ namespace BililiveRecorder.WPF.Pages
private async void RootPage_Loaded(object sender, RoutedEventArgs e) private async void RootPage_Loaded(object sender, RoutedEventArgs e)
{ {
bool first_time = true;
var recorder = RootScope.Resolve<IRecorder>(); var recorder = RootScope.Resolve<IRecorder>();
var first_time = true;
var error = string.Empty; var error = string.Empty;
string path; string path;
while (true) while (true)
{
try
{ {
CommandLineOption commandLineOption = null; CommandLineOption commandLineOption = null;
if (first_time) if (first_time)
{
// while 循环第一次运行时检查命令行参数
try
{ {
first_time = false; first_time = false;
Parser.Default Parser.Default
@ -85,34 +92,54 @@ namespace BililiveRecorder.WPF.Pages
if (!string.IsNullOrWhiteSpace(commandLineOption?.WorkDirectory)) if (!string.IsNullOrWhiteSpace(commandLineOption?.WorkDirectory))
{ {
// 如果有参数直接跳到检查路径
path = Path.GetFullPath(commandLineOption.WorkDirectory); path = Path.GetFullPath(commandLineOption.WorkDirectory);
goto check_path; }
else
{
// 无命令行参数
continue;
} }
} }
catch (Exception)
string lastdir = string.Empty; {
// 出错直接重新来,不显示 error
continue;
}
}
else
{
// 尝试读取上次选择的路径
var lastdir = string.Empty;
try try
{ {
if (File.Exists(lastdir_path)) if (File.Exists(lastdir_path))
{
lastdir = File.ReadAllText(lastdir_path).Replace("\r", "").Replace("\n", "").Trim(); lastdir = File.ReadAllText(lastdir_path).Replace("\r", "").Replace("\n", "").Trim();
} }
}
catch (Exception) { } catch (Exception) { }
var w = new WorkDirectorySelectorDialog
// 显示路径选择界面
var dialog = new WorkDirectorySelectorDialog
{ {
Error = error, Error = error,
Path = lastdir Path = lastdir
}; };
var result = await w.ShowAsync();
if (result != ContentDialogResult.Primary) if (await dialog.ShowAsync() != ContentDialogResult.Primary)
{ {
(Application.Current.MainWindow as NewMainWindow).CloseWithoutConfirmAction(); (Application.Current.MainWindow as NewMainWindow).CloseWithoutConfirmAction();
return; return;
} }
path = Path.GetFullPath(w.Path); try
check_path: { path = Path.GetFullPath(dialog.Path); }
catch (Exception)
{
error = "不支持该路径";
continue;
}
}
var config = Path.Combine(path, "config.json"); var config = Path.Combine(path, "config.json");
if (!Directory.Exists(path)) if (!Directory.Exists(path))
@ -130,18 +157,17 @@ namespace BililiveRecorder.WPF.Pages
continue; continue;
} }
// 已经选定工作目录
// 如果不是从命令行参数传入的路径,写入 lastdir_path 记录
try try
{ { if (string.IsNullOrWhiteSpace(commandLineOption?.WorkDirectory)) File.WriteAllText(lastdir_path, path); }
if (string.IsNullOrWhiteSpace(commandLineOption?.WorkDirectory))
{
File.WriteAllText(lastdir_path, path);
}
}
catch (Exception) { } catch (Exception) { }
// 检查已经在同目录运行的其他进程 // 检查已经在同目录运行的其他进程
if (SingleInstance.CheckMutex(path)) if (SingleInstance.CheckMutex(path))
{ {
// 无已经在同目录运行的进程
if (recorder.Initialize(path)) if (recorder.Initialize(path))
{ {
Model.Recorder = recorder; Model.Recorder = recorder;
@ -164,10 +190,18 @@ namespace BililiveRecorder.WPF.Pages
} }
else else
{ {
// 有已经在其他目录运行的进程,已经通知该进程,本进程退出
(Application.Current.MainWindow as NewMainWindow).CloseWithoutConfirmAction(); (Application.Current.MainWindow as NewMainWindow).CloseWithoutConfirmAction();
return; return;
} }
} }
catch (Exception ex)
{
error = "发生了未知错误";
logger.Error(ex, "选择工作目录时发生了未知错误");
continue;
}
}
} }
private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)