-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsHelper.cs
More file actions
62 lines (54 loc) · 3.22 KB
/
Copy pathSettingsHelper.cs
File metadata and controls
62 lines (54 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
namespace LocalSettingsJson;
public static class SettingsHelper
{
/// <summary>
/// Gets the name of the current application without its file extension.
/// </summary>
/// <remarks>This method attempts to retrieve the application name using the process path when available;
/// otherwise, it falls back to the friendly name of the current application domain. The result may vary depending
/// on the hosting environment and .NET runtime version.</remarks>
/// <returns>A string containing the application's name without the file extension. Returns an empty string if the
/// application name cannot be determined.</returns>
public static string GetAppName()
{
// ProcessPath (.NET 6+, full exe path)
if(!string.IsNullOrWhiteSpace(Environment.ProcessPath))
return Path.GetFileNameWithoutExtension(Environment.ProcessPath);
// fallback
return !string.IsNullOrEmpty(AppDomain.CurrentDomain.FriendlyName)
? Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName)
: "UnknownAppName";
}
/// <summary>
/// Gets the default file path for the application's settings file in the local application data folder.
/// </summary>
/// <remarks>The returned path is suitable for storing user-specific application settings. The method
/// combines the local application data folder, the application name, and the file name 'settings.json'.</remarks>
/// <returns>A string containing the full path to the default settings file. The path is located in the application's
/// subdirectory within the local application data folder.</returns>
public static string GetDefaultAppDataPath()
{
var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var appFolder = GetAppName();
return Path.Combine(localAppData, appFolder, "settings.json");
}
/// <summary>
/// Ensures that the directory for the specified file path exists, creating it if necessary.
/// </summary>
/// <remarks>This method does not create the file itself; it only ensures that the directory structure
/// required for the file exists. If the directory already exists, no action is taken.</remarks>
/// <param name="path">The full file path for which to ensure the containing directory exists. Cannot be null, empty, or whitespace.</param>
/// <exception cref="InvalidOperationException">Thrown if the specified path is null, empty, or consists only of white-space characters.</exception>
/// <exception cref="DirectoryNotFoundException">Thrown if the directory part of the specified path is null, empty, or consists only of white-space characters.</exception>
internal static void EnsureFileExists(string path)
{
// verifing path
if(string.IsNullOrWhiteSpace(path))
throw new InvalidOperationException($"Invalid FilePath: '{path ?? "<null>"}'");
// verifing dir
var dir = Path.GetDirectoryName(path);
if (string.IsNullOrWhiteSpace(dir))
throw new DirectoryNotFoundException($"Invalid directory: '{dir ?? "<null>"}'");
Directory.CreateDirectory(dir); // creates a valid dir if doesn´t exist
}
}