Last active
August 29, 2015 14:03
-
-
Save kazuakix/b9cf8b6c221e7e4da339 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO.IsolatedStorage; | |
namespace Net.Tauchi.Sue | |
{ | |
/// <summary> | |
/// アプリの設定 | |
/// </summary> | |
public class AppSettings | |
{ | |
#region 定数 | |
private const string USE_SCHEDULED_AGENT = "UseScheduledAgent"; // 定期実行エージェントを使用する | |
private const string USER_MAIL_ADDRESS = "MailAddress"; // ●用メールアドレス | |
private const string USER_PASSWORD = "Password"; // ●用パスワード | |
#endregion | |
#region メンバ変数 | |
private IsolatedStorageSettings _iso; | |
#endregion | |
#region コンストラクタ | |
public AppSettings() | |
{ | |
try | |
{ | |
_iso = IsolatedStorageSettings.ApplicationSettings; | |
} | |
catch (Exception ex) | |
{ | |
System.Diagnostics.Debug.Assert(false, ex.Message); | |
} | |
} | |
#endregion | |
#region 設定更新 | |
public bool AddOrUpdateValue(string key, object value) | |
{ | |
bool valueIsChanged = false; | |
try | |
{ | |
if (value != _iso[key]) | |
{ | |
_iso[key] = value; | |
valueIsChanged = true; | |
} | |
} | |
catch (KeyNotFoundException) | |
{ | |
_iso.Add(key, value); | |
valueIsChanged = true; | |
} | |
catch (ArgumentException) | |
{ | |
_iso.Add(key, value); | |
valueIsChanged = true; | |
} | |
catch (Exception ex) | |
{ | |
System.Diagnostics.Debug.Assert(false, ex.Message); | |
} | |
return valueIsChanged; | |
} | |
#endregion | |
#region 設定取得 | |
public T GetValueOrDefault<T>(string key, T defaultValue) | |
{ | |
T value; | |
try | |
{ | |
value = (T)_iso[key]; | |
} | |
catch (KeyNotFoundException) | |
{ | |
value = defaultValue; | |
_iso.Add(key, value); | |
Save(); | |
} | |
catch (ArgumentException) | |
{ | |
value = defaultValue; | |
_iso.Add(key, value); | |
Save(); | |
} | |
catch (Exception ex) | |
{ | |
value = defaultValue; | |
System.Diagnostics.Debug.Assert(false, ex.Message); | |
} | |
return value; | |
} | |
#endregion | |
#region 保存 | |
public void Save() | |
{ | |
_iso.Save(); | |
} | |
#endregion | |
// 以下、公開する設定値 | |
#region 定期実行エージェントを使用するか | |
public bool UseScheduledAgent | |
{ | |
get | |
{ | |
return GetValueOrDefault<bool>(USE_SCHEDULED_AGENT, false); | |
} | |
set | |
{ | |
AddOrUpdateValue(USE_SCHEDULED_AGENT, value); | |
Save(); | |
} | |
} | |
#endregion | |
#region ●用メールアドレス | |
public string UserMailAddress | |
{ | |
get | |
{ | |
return GetValueOrDefault<string>(USER_MAIL_ADDRESS, ""); | |
} | |
set | |
{ | |
AddOrUpdateValue(USER_MAIL_ADDRESS, value); | |
Save(); | |
} | |
} | |
#endregion | |
#region ●用パスワード | |
public string UserPassword | |
{ | |
get | |
{ | |
return GetValueOrDefault<string>(USER_PASSWORD, ""); | |
} | |
set | |
{ | |
AddOrUpdateValue(USER_PASSWORD, value); | |
Save(); | |
} | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment