store last selected sln file
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.Build.Locator;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MudBlazor.Services;
|
||||
using Photino.Blazor;
|
||||
using SharpIDE.Photino.Models;
|
||||
using SharpIDE.Photino.Services;
|
||||
|
||||
namespace SharpIDE.Photino;
|
||||
@@ -16,6 +18,7 @@ public class Program
|
||||
appBuilder.Services.AddLogging();//
|
||||
appBuilder.Services.AddMudServices();
|
||||
appBuilder.Services.AddSingleton<RefreshOpenFileService>();
|
||||
appBuilder.Services.AddSingleton<AppState>();
|
||||
|
||||
appBuilder.RootComponents.Add<App>("app");
|
||||
|
||||
@@ -35,6 +38,21 @@ public class Program
|
||||
refreshOpenFileService.InvokeRefreshOpenFile();
|
||||
};
|
||||
|
||||
var configFilePath = GetConfigFilePath();
|
||||
|
||||
using var scope = app.Services.CreateScope();
|
||||
var appState = scope.ServiceProvider.GetRequiredService<AppState>();
|
||||
|
||||
LoadAppStateFromConfigFile(appState, configFilePath);
|
||||
|
||||
app.MainWindow.RegisterWindowClosingHandler((sender, eventArgs) =>
|
||||
{
|
||||
using var stream = File.Create(configFilePath);
|
||||
JsonSerializer.Serialize(stream, appState);
|
||||
stream.Flush();
|
||||
return false;
|
||||
});
|
||||
|
||||
AppDomain.CurrentDomain.UnhandledException += (sender, error) =>
|
||||
{
|
||||
app.MainWindow.ShowMessage("Fatal exception", error.ExceptionObject.ToString());
|
||||
@@ -45,4 +63,32 @@ public class Program
|
||||
|
||||
app.Run();
|
||||
}
|
||||
|
||||
private static string GetConfigFilePath()
|
||||
{
|
||||
var folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
||||
var configFolder = Path.Combine(folder, "SharpIDE.PhotinoConfig");
|
||||
Directory.CreateDirectory(configFolder);
|
||||
var configFilePath = Path.Combine(configFolder, "config.json");
|
||||
return configFilePath;
|
||||
}
|
||||
|
||||
private static void LoadAppStateFromConfigFile(AppState appState, string configFilePath)
|
||||
{
|
||||
if (File.Exists(configFilePath) is false)
|
||||
{
|
||||
File.WriteAllText(configFilePath, string.Empty);
|
||||
}
|
||||
|
||||
using var stream = File.OpenRead(configFilePath);
|
||||
if (stream.Length is 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var deserializedAppState = JsonSerializer.Deserialize<AppState>(stream);
|
||||
if (deserializedAppState is not null)
|
||||
{
|
||||
appState.SolutionFilePath = deserializedAppState.SolutionFilePath;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user