Skip to content

Commit 7765b4c

Browse files
committed
#137 if no write permissions, use ApplicationData folder
1 parent 8b6ef6e commit 7765b4c

File tree

5 files changed

+57
-14
lines changed

5 files changed

+57
-14
lines changed

src/PicView.Core/Config/SettingsHelper.cs

+30-10
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,19 @@ public static async Task LoadSettingsAsync()
2424
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/UserSettings.json");
2525
if (File.Exists(path))
2626
{
27-
await PerformRead(path).ConfigureAwait(false);
28-
}
29-
else
30-
{
31-
// TODO test saving location for macOS https://johnkoerner.com/csharp/special-folder-values-on-windows-versus-mac/
32-
var appData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Ruben2776/PicView/Config/UserSettings.json");
33-
if (File.Exists(appData))
27+
try
3428
{
35-
await PerformRead(appData).ConfigureAwait(false);
29+
await PerformRead(path).ConfigureAwait(false);
3630
}
37-
else
31+
catch (Exception)
3832
{
39-
SetDefaults();
33+
await Retry().ConfigureAwait(false);
4034
}
4135
}
36+
else
37+
{
38+
await Retry().ConfigureAwait(false);
39+
}
4240
}
4341
catch (Exception ex)
4442
{
@@ -47,6 +45,28 @@ public static async Task LoadSettingsAsync()
4745
#endif
4846
SetDefaults();
4947
}
48+
return;
49+
50+
async Task Retry()
51+
{
52+
// TODO test saving location for macOS https://johnkoerner.com/csharp/special-folder-values-on-windows-versus-mac/
53+
var appData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Ruben2776/PicView/Config/UserSettings.json");
54+
if (File.Exists(appData))
55+
{
56+
try
57+
{
58+
await PerformRead(appData).ConfigureAwait(false);
59+
}
60+
catch (Exception)
61+
{
62+
SetDefaults();
63+
}
64+
}
65+
else
66+
{
67+
SetDefaults();
68+
}
69+
}
5070
}
5171

5272
private static void SetDefaults()

src/PicView.Core/Navigation/FileHistory.cs

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics;
2+
using System.Runtime;
23

34
namespace PicView.Core.Navigation;
45

@@ -28,6 +29,14 @@ public FileHistory()
2829
}
2930
catch (Exception e)
3031
{
32+
try
33+
{
34+
_fileLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Ruben2776/PicView/Config/recent.txt");
35+
}
36+
catch (Exception)
37+
{
38+
Trace.WriteLine($"{nameof(FileHistory)} exception, \n{e.Message}");
39+
}
3140
#if DEBUG
3241
Trace.WriteLine($"{nameof(FileHistory)} exception, \n{e.Message}");
3342
#endif

src/PicView.WPF/App.xaml.cs

+14-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,20 @@ public partial class App
99
{
1010
protected override void OnStartup(StartupEventArgs e)
1111
{
12-
ProfileOptimization.SetProfileRoot(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/"));
13-
ProfileOptimization.StartProfile("ProfileOptimization");
12+
Task.Run((() =>
13+
{
14+
try
15+
{
16+
ProfileOptimization.SetProfileRoot(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/"));
17+
ProfileOptimization.StartProfile("ProfileOptimization");
18+
}
19+
catch (Exception)
20+
{
21+
ProfileOptimization.SetProfileRoot(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Ruben2776/PicView/Config/"));
22+
ProfileOptimization.StartProfile("ProfileOptimization");
23+
}
24+
}));
25+
1426
DispatcherUnhandledException += App_DispatcherUnhandledException;
1527
}
1628

src/PicView.WPF/ChangeImage/FileHistoryNavigation.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ internal static async Task OpenLastFileAsync()
5252
{
5353
if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/recent.txt")) == false)
5454
{
55-
return;
55+
if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Ruben2776/PicView/Config/recent.txt")) == false)
56+
{
57+
return;
58+
}
5659
}
5760

5861
await ConfigureWindows.GetMainWindow.Dispatcher.InvokeAsync(() =>

src/PicView.WPF/ChangeImage/FileUpdateNavigation.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using PicView.Core.Config;
22
using PicView.Core.FileHandling;
33
using PicView.Core.Gallery;
4-
using PicView.Core.Navigation;
54
using PicView.WPF.FileHandling;
65
using PicView.WPF.ImageHandling;
76
using PicView.WPF.PicGallery;

0 commit comments

Comments
 (0)