From ed9239dd8ab8c40e222cc1359e4e24cc2be90236 Mon Sep 17 00:00:00 2001 From: Dmitry Yarygin Date: Mon, 18 Aug 2025 01:04:58 +0200 Subject: [PATCH] Add AppData helper for user config --- FileManager.csproj | 5 +--- MainWindow.xaml.cs | 2 +- Models/Settings.cs | 3 ++- README.md | 2 +- Utils/AppData.cs | 55 ++++++++++++++++++++++++++++++++++++++++++++ Utils/LinkStorage.cs | 4 ++-- links.json | 1 - 7 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 Utils/AppData.cs delete mode 100644 links.json diff --git a/FileManager.csproj b/FileManager.csproj index 1f13254..9e3082a 100644 --- a/FileManager.csproj +++ b/FileManager.csproj @@ -1,4 +1,4 @@ - + WinExe @@ -14,9 +14,6 @@ PreserveNewest - - PreserveNewest - diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 38edb97..39a7224 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -375,7 +375,7 @@ private void OpenLynkr_Click(object sender, RoutedEventArgs e) private void OpenSettingsIni_Click(object sender, RoutedEventArgs e) { Logger.Log("Open Settings ini clicked"); - var configPath = Path.Combine(AppContext.BaseDirectory, "dsfm.ini"); + var configPath = Settings.ConfigPath; try { Process.Start(new ProcessStartInfo("notepad.exe", configPath) diff --git a/Models/Settings.cs b/Models/Settings.cs index a5deca6..8d7f244 100644 --- a/Models/Settings.cs +++ b/Models/Settings.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; using System.IO; +using DamnSimpleFileManager.Utils; namespace DamnSimpleFileManager { internal static class Settings { - private static readonly string ConfigPath = Path.Combine(AppContext.BaseDirectory, "dsfm.ini"); + internal static string ConfigPath => AppData.GetPath("dsfm.ini"); private static readonly Dictionary Values = new(); public static bool ShowHiddenFiles => diff --git a/README.md b/README.md index 3e4724b..ab30050 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ The application is intentionally minimalistic and aims to offer a straightforwar ## Configuration -`dsfm.ini` is created automatically next to the application. +`dsfm.ini` is stored per-user in the application's data folder (e.g., `%APPDATA%\\DamnSimpleFileManager`). Set `hidden_files=false` to hide files and folders marked as hidden. Set `copy_confirmation=false` to copy files and folders without confirmation. Set `move_confirmation=false` to move files and folders without confirmation. diff --git a/Utils/AppData.cs b/Utils/AppData.cs new file mode 100644 index 0000000..a5f6a7e --- /dev/null +++ b/Utils/AppData.cs @@ -0,0 +1,55 @@ +using System; +using System.IO; +using System.Security.AccessControl; +using System.Security.Principal; + +namespace DamnSimpleFileManager.Utils +{ + public static class AppData + { + public static readonly string DirectoryPath; + + static AppData() + { + var basePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + DirectoryPath = Path.Combine(basePath, "DamnSimpleFileManager"); + EnsureDirectory(); + } + + private static void EnsureDirectory() + { + if (Directory.Exists(DirectoryPath)) return; + + var userSid = WindowsIdentity.GetCurrent().User!; + var security = new DirectorySecurity(); + security.SetOwner(userSid); + security.AddAccessRule(new FileSystemAccessRule( + userSid, + FileSystemRights.FullControl, + InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, + PropagationFlags.None, + AccessControlType.Allow)); + + Directory.CreateDirectory(DirectoryPath, security); + } + + public static string GetPath(string fileName) + { + var path = Path.Combine(DirectoryPath, fileName); + if (!File.Exists(path)) + { + using (File.Create(path)) { } + var userSid = WindowsIdentity.GetCurrent().User!; + var security = new FileSecurity(); + security.SetOwner(userSid); + security.AddAccessRule(new FileSystemAccessRule( + userSid, + FileSystemRights.FullControl, + AccessControlType.Allow)); + File.SetAccessControl(path, security); + } + return path; + } + } +} + diff --git a/Utils/LinkStorage.cs b/Utils/LinkStorage.cs index 87aa6f6..9335752 100644 --- a/Utils/LinkStorage.cs +++ b/Utils/LinkStorage.cs @@ -6,12 +6,12 @@ namespace DamnSimpleFileManager.Utils { public static class LinkStorage { - private const string FileName = "links.json"; + private static readonly string FileName = AppData.GetPath("links.json"); public static List Load() { - if (!File.Exists(FileName)) return new List(); var json = File.ReadAllText(FileName); + if (string.IsNullOrWhiteSpace(json)) return new List(); return JsonSerializer.Deserialize>(json) ?? new List(); } diff --git a/links.json b/links.json deleted file mode 100644 index fe51488..0000000 --- a/links.json +++ /dev/null @@ -1 +0,0 @@ -[]