fix: application data dir may not have been created before crash (#161)

This commit is contained in:
leo 2024-06-03 09:44:12 +08:00
parent f1f54bf6fe
commit 49ce07443e
4 changed files with 8 additions and 12 deletions

View file

@ -62,9 +62,7 @@ namespace SourceGit
builder.Append(ex.StackTrace); builder.Append(ex.StackTrace);
var time = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); var time = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
var file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), var file = Path.Combine(Native.OS.DataDir, $"crash_{time}.log");
"SourceGit",
$"crash_{time}.log");
File.WriteAllText(file, builder.ToString()); File.WriteAllText(file, builder.ToString());
} }
} }

View file

@ -25,7 +25,7 @@ namespace SourceGit.Models
static AvatarManager() static AvatarManager()
{ {
_storePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SourceGit", "avatars"); _storePath = Path.Combine(Native.OS.DataDir, "avatars");
if (!Directory.Exists(_storePath)) if (!Directory.Exists(_storePath))
Directory.CreateDirectory(_storePath); Directory.CreateDirectory(_storePath);

View file

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using Avalonia; using Avalonia;
@ -20,6 +21,7 @@ namespace SourceGit.Native
void OpenWithDefaultEditor(string file); void OpenWithDefaultEditor(string file);
} }
public static readonly string DataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SourceGit");
public static string GitExecutable { get; set; } = string.Empty; public static string GitExecutable { get; set; } = string.Empty;
public static List<Models.ExternalTool> ExternalTools { get; set; } = new List<Models.ExternalTool>(); public static List<Models.ExternalTool> ExternalTools { get; set; } = new List<Models.ExternalTool>();
@ -72,6 +74,9 @@ namespace SourceGit.Native
public static void SetupApp(AppBuilder builder) public static void SetupApp(AppBuilder builder)
{ {
if (!Directory.Exists(DataDir))
Directory.CreateDirectory(DataDir);
_backend.SetupApp(builder); _backend.SetupApp(builder);
} }

View file

@ -459,10 +459,6 @@ namespace SourceGit.ViewModels
public static void Save() public static void Save()
{ {
var dir = Path.GetDirectoryName(_savePath);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
var data = JsonSerializer.Serialize(_instance, JsonCodeGen.Default.Preference); var data = JsonSerializer.Serialize(_instance, JsonCodeGen.Default.Preference);
File.WriteAllText(_savePath, data); File.WriteAllText(_savePath, data);
} }
@ -515,10 +511,7 @@ namespace SourceGit.ViewModels
} }
private static Preference _instance = null; private static Preference _instance = null;
private static readonly string _savePath = Path.Combine( private static readonly string _savePath = Path.Combine(Native.OS.DataDir, "preference.json");
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"SourceGit",
"preference.json");
private string _locale = "en_US"; private string _locale = "en_US";
private string _theme = "Default"; private string _theme = "Default";