2016-09-09 17 views
1

おはよう、私はDateTime値をアプリの設定に保存しようとしています。アプリ設定の現在の日付時間を保存する

私の設定テーブルでは、私はInstallationDatePreviousDateを示しています。

名|タイプ|適用範囲|バリュー

InstallationDate - のSystem.DateTime - ユーザー -

PreviousDate - のSystem.DateTime - ユーザー -

私は最初の実行時にInstallationDateを挿入しようとしていますForm_ClosingのPreviousDateです。

private void frmMainMenu_Load(object sender, EventArgs e) 
    { 
     int firstU = Properties.Settings.Default.FirstUse; 
     int one = 1; 

     if (firstU == 0) 
     { 
      Properties.Settings.Default["FirstUse"] = one; 
      Properties.Settings.Default.Save(); 
     } 


     Properties.Settings.Default["InstallationDate"] = DateTime.Now.ToShortDateString(); 
     Properties.Settings.Default.Save(); 

    } 


private void frmMainMenu_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     Properties.Settings.Default["PreviousDate"] = DateTime.Now.ToShortDateString(); 
     Properties.Settings.Default.Save(); 
    } 

しかし、問題は今、その例外

をtrowing「設定プロパティが 『InstallationDate』非互換の型です。」

+1

'System.DateTime'型を使用してプロパティを定義したので、プロパティに' DateTime.Now'を割り当てる必要があります。 –

+2

それ以上、 'DateTime.UtcNow' ... –

答えて

3

あなたは書く:

Properties.Settings.Default["InstallationDate"] = DateTime.Now.ToShortDateString(); 

をあなたはInstallationDateDateTimeですが、それゆえ型が一致しない、フィールドで(ToShortDateStringを使用して)stringを設定しようと言います。

+0

私はちょうど変更し、それは例外を発生させませんでしたが、DateTime値はApp.Settingsファイルに保存されていません。 –

関連する問題