連絡先の詳細を保持するWindowsフォームデスクトップアプリケーションがあります。アプリケーションのアプリケーションを閉じるときに以下のように格納する連絡先データを、アプリケーションを閉じて再オープンした後にProperties.Settings.Default.ApplicationDataを保持する方法
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
Properties.Settings.Default.ApplicationData = mydata;
Properties.Settings.Default.Save();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
アプリケーションが起動すると、それは以下のようにデータをロードし、
try
{
this.mydata = (DataHandeler) Properties.Settings.Default.ApplicationData;
}
catch (NullReferenceException)
{
mydata = new DataHandeler();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
mydata = new DataHandeler();
}
SettingsSerializeAsは、以下のようにSettings.Designer.csに
が追加されています[global :: System.Diagnostics.DebuggerNonUserCodeAttribute()] [グローバル:: System.Configuration.UserScopedSettingAttribute()]グローバル:: System.Configuration.SettingsSerializeAs(System.Configur ation.SettingsSerializeAs.Binary)]
public object ApplicationData {
get {
return ((object)(this["ApplicationData"]));
}
set {
this["ApplicationData"] = value;
}
しかし、アプリケーションは私が閉じるたびに、再オープンしたアプリケーションの後、既に保存されたデータを回復することはできません。アプリケーションがデータをロードしようとすると、NullreferenceExceptionがスローされます。どのようにしてデータを回復できますか?ちょうどそれが新しい設定を使用するように、これはapp.configをファイルに保存されます。この
Properties.Settings.Default.AppLaststarted = date;
Properties.Settings.Default.Save();
ようsetingsに
を保存するメソッドを呼び出して設定を変更した後
"回復できません"を定義してください:例外はありますか?メッセージとは何ですか? –