OSDN Git Service

Merge branch 'スレッド間同期の見直し' into develop
[strokestylet/CsWin10Desktop3.git] / SSTFEditor / ConfigXML.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Diagnostics;
6 using System.Xml.Serialization;
7 using System.Windows.Forms;
8
9 namespace SSTFEditor
10 {
11         public class Config
12         {
13                 // プロパティ(保存される)
14
15                 public bool AutoFocus = true;
16                 public bool ShowRecentUsedFiles = true;
17                 public int MaxOfUsedRecentFiles = 10;
18                 public List<string> RecentUsedFiles = new List<string>();
19                 public string ViewerPath = "";
20
21                 // メソッド(保存されない)
22
23                 public static Config 読み込む( string ファイル名 )
24                 {
25                         Config config = null;
26
27                         try
28                         {
29                                 config = FDK.XML.Serializer.ファイルをデシリアライズしてインスタンスを生成する<Config>( ファイル名 );
30                         }
31                         catch( Exception )
32                         {
33                                 config = new Config();  // 読み込めなかったら新規作成する。
34                         }
35
36                         return config;
37                 }
38                 public void 保存する( string ファイル名 )
39                 {
40                         try
41                         {
42                                 FDK.XML.Serializer.インスタンスをシリアライズしてファイルに保存する( ファイル名, this );
43                         }
44                         catch( Exception e )
45                         {
46                                 MessageBox.Show( $"ファイルの保存に失敗しました。[{ファイル名}]\n--------\n{e.ToString()}" );
47                         }
48                 }
49                 public void ファイルを最近使ったファイルの一覧に追加する( string ファイル名 )
50                 {
51                         // 絶対パスを取得する。
52                         ファイル名 = Path.GetFullPath( ファイル名 );
53
54                         // 一覧に同じ文字列があったら一覧から削除する。
55                         this.RecentUsedFiles.RemoveAll( ( path ) => { return path.Equals( ファイル名 ); } );
56
57                         // 一覧の先頭に登録する。
58                         this.RecentUsedFiles.Insert( 0, ファイル名 );
59
60                         // 10個以上は記録しない。
61                         if( this.RecentUsedFiles.Count > 10 )
62                         {
63                                 int 超えてる数 = this.RecentUsedFiles.Count - 10;
64
65                                 for( int i = 超えてる数; i > 0; i-- )
66                                         this.RecentUsedFiles.RemoveAt( 10 + i - 1 );
67                         }
68                 }
69         }
70 }