-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.cs
More file actions
90 lines (80 loc) · 2.57 KB
/
Copy pathSettings.cs
File metadata and controls
90 lines (80 loc) · 2.57 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.IO;
using System.Text.Json;
namespace SquareSnap
{
public class Settings
{
private static readonly string SettingsFilePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"SquareSnap",
"settings.json");
public string? DefaultSaveLocation { get; set; }
public int NextFileNumber { get; set; } = 1;
// Get the next file name in the format SquareCap-YYYYMMDD###
public string GetNextFileName()
{
string dateStr = DateTime.Now.ToString("yyyyMMdd");
string numberStr = NextFileNumber.ToString("D3"); // Pad with leading zeros to 3 digits
// Increment the file number for next time
NextFileNumber++;
Save();
return $"SquareCap-{dateStr}{numberStr}";
}
// Singleton instance
private static Settings? _instance;
public static Settings Instance
{
get
{
if (_instance == null)
{
_instance = Load();
}
return _instance;
}
}
// Load settings from file
private static Settings Load()
{
try
{
// Create directory if it doesn't exist
string? directory = Path.GetDirectoryName(SettingsFilePath);
if (directory != null && !Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
// Load settings from file if it exists
if (File.Exists(SettingsFilePath))
{
string json = File.ReadAllText(SettingsFilePath);
var settings = JsonSerializer.Deserialize<Settings>(json);
if (settings != null)
{
return settings;
}
}
}
catch (Exception)
{
// Ignore errors and return default settings
}
// Return default settings
return new Settings();
}
// Save settings to file
public void Save()
{
try
{
string json = JsonSerializer.Serialize(this);
File.WriteAllText(SettingsFilePath, json);
}
catch (Exception)
{
// Ignore errors
}
}
}
}