Skip to content

Commit d757216

Browse files
committed
v0.2.1.2 読み取りエラーが起きにくくした
1 parent a4fe1b9 commit d757216

2 files changed

Lines changed: 89 additions & 5 deletions

File tree

GhostDist/Forms/MainForm.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,21 @@ private void LoadConfiguration()
5555
catch (Exception ex)
5656
{
5757
_configLoadFailed = true; // 読み込み失敗時は保存を防止
58-
MessageBox.Show($"設定ファイルの読み込みに失敗しました: {ex.Message}\n\n設定ファイルのパス: {_iniPath}\n\n読み込みに失敗したため、設定の保存は無効になります。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
58+
59+
var message = new StringBuilder();
60+
message.AppendLine("設定ファイルの読み込みに失敗しました。");
61+
message.AppendLine();
62+
message.AppendLine(ex.Message);
63+
message.AppendLine();
64+
message.AppendLine("【診断情報】");
65+
message.AppendLine($"実行ファイル: {Application.ExecutablePath}");
66+
message.AppendLine($"起動パス: {Application.StartupPath}");
67+
message.AppendLine($"設定ファイル: {_iniPath}");
68+
message.AppendLine();
69+
message.AppendLine("読み込みに失敗したため、設定の保存は無効になります。");
70+
message.AppendLine("既存の設定ファイルは上書きされません。");
71+
72+
MessageBox.Show(message.ToString(), "設定ファイル読み込みエラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
5973
}
6074
}
6175

GhostDist/Services/ConfigurationService.cs

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using GhostDist.Models;
77
using IniParser;
88
using IniParser.Model;
9+
using IniParser.Parser;
910

1011
namespace 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

Comments
 (0)