Repository navigation
Expand file tree
/
Copy pathConfig.cs
More file actions
93 lines (80 loc) · 2.64 KB
/
Copy pathConfig.cs
File metadata and controls
93 lines (80 loc) · 2.64 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCClockSenderApp
{
/// <summary>
/// 設定クラス
/// </summary>
public class Config
{
#region フィールド定義
public Person Person { get; set; }
#endregion
/// <summary>
/// コンストラクタ
/// </summary>
/// <returns></returns>
public Config()
{
this.Person = new Person();
}
/// <summary>
/// 設定ファイルのフルパス取得
/// </summary>
/// <returns>設定ファイルのフルパス</returns>
public static string GetConfigFilePath()
{
//実行ファイルのフルパス取得
string appFliePath = System.Reflection.Assembly.GetEntryAssembly().Location;
//実行ファイルのフルパス拡張子を変えて返す
return System.Text.RegularExpressions.Regex.Replace(
appFliePath,
"exe",
"json",
System.Text.RegularExpressions.RegexOptions.IgnoreCase );
}
///<summary>
///設定読み込み
/// </summary>
/// <returns></returns>
public static Config ReadConfig()
{
//設定ファイルのフルパス取得
string configFile = GetConfigFilePath();
if (File.Exists(configFile) == false)
{
//設定ファイルなし
return null;
}
using (var reader = new StreamReader(configFile, Encoding.UTF8))
{
//設定ファイル読み込み
string buf = reader.ReadToEnd();
//デシリアライズして返す
var js = new System.Web.Script.Serialization.JavaScriptSerializer();
return js.Deserialize<Config>(buf);
}
}
///<summary>
///設定書き込み
///<summary>
///<param name="cfg"></param>
public static void WriteConfig(Config cfg)
{
//シリアライズ
var js = new System.Web.Script.Serialization.JavaScriptSerializer();
string buf = js.Serialize(cfg);
//設定ファイルのフルパス取得
string configFile = GetConfigFilePath();
using (var writer = new StreamWriter(configFile, false, Encoding.UTF8))
{
//設定ファイルに書き込む
writer.Write(buf);
}
}
}
}