-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.cs
More file actions
95 lines (85 loc) · 3.53 KB
/
Settings.cs
File metadata and controls
95 lines (85 loc) · 3.53 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
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using System.Windows.Forms;
namespace BlogPingSender
{
/// <summary>
/// アプリケーションの設定を管理するクラス
/// </summary>
public class Settings
{
/// <summary>
/// 監視対象のブログリスト
/// </summary>
public List<BlogInfo> MonitoredBlogs { get; set; } = new List<BlogInfo>();
/// <summary>
/// Ping送信先のURLリスト
/// </summary>
public List<string> PingUrls { get; set; } = new List<string> { "http://rpc.pingomatic.com/" };
/// <summary>
/// 更新をチェックする間隔(分)
/// </summary>
public int CheckIntervalMinutes { get; set; } = 60;
/// <summary>
/// 閉じるボタンでタスクトレイに常駐させるかどうか
/// </summary>
public bool MinimizeToTrayOnClose { get; set; } = true;
/// <summary>
/// Windows起動時に自動実行するかどうか
/// </summary>
public bool StartWithWindows { get; set; } = false;
/// <summary>
/// アプリ起動時に自動で監視を開始するかどうか
/// </summary>
public bool StartMonitoringOnLaunch { get; set; } = true;
/// <summary>
/// 設定ファイルのフルパス
/// </summary>
public static readonly string settingsFilePath = Path.Combine(Application.StartupPath, "settings.json");
/// <summary>
/// 現在の設定内容をJSONファイルに保存する
/// </summary>
public void Save()
{
try
{
// 日本語が文字化けしないようにエンコーダーを指定
var options = new JsonSerializerOptions
{
WriteIndented = true, // 人が読みやすいようにインデントを付ける
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};
string jsonString = JsonSerializer.Serialize(this, options);
File.WriteAllText(settingsFilePath, jsonString);
}
catch (Exception ex)
{
MessageBox.Show($"設定の保存に失敗しました。\n{ex.Message}", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// JSONファイルから設定を読み込む
/// </summary>
/// <returns>読み込まれた設定オブジェクト</returns>
public static Settings Load()
{
// ファイルが存在しない場合は、デフォルト値で新しい設定オブジェクトを返す
if (!File.Exists(settingsFilePath)) return new Settings();
try
{
string jsonString = File.ReadAllText(settingsFilePath);
// JSONからデシリアライズして返す。失敗した場合はnullになるので、その際はnew Settings()を返す
return JsonSerializer.Deserialize<Settings>(jsonString) ?? new Settings();
}
catch (Exception ex)
{
MessageBox.Show($"設定の読み込みに失敗しました。デフォルト設定を使用します。\n{ex.Message}", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
return new Settings();
}
}
}
}