66using GhostDist . Models ;
77using IniParser ;
88using IniParser . Model ;
9+ using IniParser . Parser ;
910
1011namespace GhostDist . Services
1112{
@@ -21,6 +22,11 @@ public ConfigurationService(string iniPath)
2122 _iniPath = iniPath ;
2223 }
2324
25+ /// <summary>
26+ /// 設定ファイルのパスを取得
27+ /// </summary>
28+ public string IniPath => _iniPath ;
29+
2430 /// <summary>
2531 /// すべての設定を一括読み込み
2632 /// </summary>
@@ -31,16 +37,50 @@ public void LoadAll(out bool isLog, out bool noLogWindow, out FtpConfiguration c
3137 commonFtp = new FtpConfiguration ( ) ;
3238 projects = new List < ProjectSettings > ( ) ;
3339
34- if ( ! File . Exists ( _iniPath ) )
40+ // パスの正規化
41+ string fullPath ;
42+ try
43+ {
44+ fullPath = Path . GetFullPath ( _iniPath ) ;
45+ }
46+ catch ( Exception ex )
47+ {
48+ throw new Exception ( $ "設定ファイルのパスが無効です: { _iniPath } \n 詳細: { ex . Message } ", ex ) ;
49+ }
50+
51+ // ファイルの存在確認(より詳細なチェック)
52+ if ( ! File . Exists ( fullPath ) )
3553 {
3654 // ファイルが存在しない場合は正常(初回起動)
3755 return ;
3856 }
3957
58+ // ファイル情報を取得(エラー時の診断用)
59+ FileInfo fileInfo ;
4060 try
4161 {
42- var parser = new FileIniDataParser ( ) ;
43- var data = parser . ReadFile ( _iniPath , Encoding . GetEncoding ( "Shift_JIS" ) ) ;
62+ fileInfo = new FileInfo ( fullPath ) ;
63+ }
64+ catch ( Exception ex )
65+ {
66+ throw new Exception ( $ "設定ファイルの情報を取得できません: { fullPath } \n 詳細: { ex . Message } ", ex ) ;
67+ }
68+
69+ try
70+ {
71+ // ファイルを共有読み取りモードで読み込み
72+ string iniContent ;
73+ var encoding = Encoding . GetEncoding ( "Shift_JIS" ) ;
74+
75+ using ( var fileStream = new FileStream ( fullPath , FileMode . Open , FileAccess . Read , FileShare . Read ) )
76+ using ( var reader = new StreamReader ( fileStream , encoding ) )
77+ {
78+ iniContent = reader . ReadToEnd ( ) ;
79+ }
80+
81+ // 文字列からパース
82+ var parser = new IniDataParser ( ) ;
83+ var data = parser . Parse ( iniContent ) ;
4484
4585 // 一般設定
4686 if ( data . Sections . ContainsSection ( "General" ) )
@@ -103,9 +143,39 @@ public void LoadAll(out bool isLog, out bool noLogWindow, out FtpConfiguration c
103143 }
104144 }
105145 }
146+ catch ( UnauthorizedAccessException ex )
147+ {
148+ throw new Exception (
149+ $ "設定ファイルへのアクセスが拒否されました。\n " +
150+ $ "パス: { fullPath } \n " +
151+ $ "サイズ: { fileInfo . Length : N0} bytes\n " +
152+ $ "詳細: { ex . Message } ", ex ) ;
153+ }
154+ catch ( IOException ex )
155+ {
156+ throw new Exception (
157+ $ "設定ファイルの読み込み中にI/Oエラーが発生しました。\n " +
158+ $ "パス: { fullPath } \n " +
159+ $ "サイズ: { fileInfo . Length : N0} bytes\n " +
160+ $ "ネットワークドライブの場合は接続を確認してください。\n " +
161+ $ "詳細: { ex . Message } ", ex ) ;
162+ }
163+ catch ( IniParser . Exceptions . ParsingException ex )
164+ {
165+ throw new Exception (
166+ $ "設定ファイルの形式が不正です。\n " +
167+ $ "パス: { fullPath } \n " +
168+ $ "サイズ: { fileInfo . Length : N0} bytes\n " +
169+ $ "詳細: { ex . Message } ", ex ) ;
170+ }
106171 catch ( Exception ex )
107172 {
108- throw new Exception ( $ "設定ファイルの読み込みに失敗しました: { ex . Message } ", ex ) ;
173+ throw new Exception (
174+ $ "設定ファイルの読み込みに失敗しました。\n " +
175+ $ "パス: { fullPath } \n " +
176+ $ "サイズ: { fileInfo . Length : N0} bytes\n " +
177+ $ "例外の種類: { ex . GetType ( ) . Name } \n " +
178+ $ "詳細: { ex . Message } ", ex ) ;
109179 }
110180 }
111181
0 commit comments