2016-08-12 4 views
0

後でReadFile()関数で使用できるようにファイルのファイルパスを取得するコードを記述しようとしましたが、いくつかの問題があります。MessageBoxの問題で再試行ボタン機能を実装する(C#)

私の目的は、再試行と閉じるボタンの両方を機能させたエラーメッセージを出力するMessageBoxを実装することです。

次のコードを実行すると、単にtryブロックがループされ、catchブロックは入力されません。これは、ファイルを選択しなかった場合や、OpenFileDialogでキャンセルをクリックした場合の処理​​です。私はsimilar questionにコードを適応しようとしているが、ロジックは私のコンテキストで動作しない理由を理解するために苦労しています

public static string GetFile() { 
    MyLog.Write(@"Begin OpenFileDialog Process", LogFormat.Evaluate); 
    var path = _lastFilePath != string.Empty ? _lastFilePath : GetBaseDirectory(); 
    if (path == null || !Directory.Exists(path)) 
     path = Assembly.GetExecutingAssembly().CodeBase; 

    var dialog = new OpenFileDialog { 
     InitialDirectory = path, 
     Filter = @"Text|*.txt|All|*.*", 
     RestoreDirectory = true 
    }; 

    var result = DialogResult.Retry; 
    while (result == DialogResult.Retry) { 
     try { 
      if (dialog.ShowDialog() != DialogResult.OK) { 
       MyLog.Write(@"File Retrieval was Unsuccessful", LogFormat.Result); 
      } else { 
       MyLog.Write($"FilePath: {dialog.FileName}", LogFormat.Process); 
       MyLog.Write(@"File Retrieval was Successful", LogFormat.Result); 
       _lastFilePath = Path.GetDirectoryName(dialog.FileName); 
       return dialog.FileName; 
      } 
     } catch when (result == DialogResult.Retry) { 
      MyLog.Write("No File Selected", LogFormat.Error); 
      result = MessageBox.Show(@"Please select a file..", @"No File Selected!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation); 
      if (result == DialogResult.Abort) throw; 
      return null; 
     } 
    } 
    return null; 
} 

以下

は..私は現在使用しているコードです。

私が間違って行っていることはありますか?

EDIT: エフライムの答えを使用して、私が働くようで、次を思い付くことができました。..

// USED TO RETRIEVE THE FILENAME IN A OPENFILEDIALOG 
public static string GetFile() { 
    MyLog.Write(@"Begin OpenFileDialog Process", LogFormat.Evaluate); 
    var path = _lastFilePath != string.Empty ? _lastFilePath : GetBaseDirectory(); 
    if (path == null || !Directory.Exists(path)) 
     path = Assembly.GetExecutingAssembly().CodeBase; 

    var dialog = new OpenFileDialog { 
     InitialDirectory = path, 
     Filter = @"Text|*.txt|All|*.*", 
     RestoreDirectory = true 
    }; 

    var result = DialogResult.Retry; 
    while (result == DialogResult.Retry) { 
     if (dialog.ShowDialog() != DialogResult.OK) { 
      MyLog.Write(@"File Retrieval was Unsuccessful", LogFormat.Result); 
      MyLog.Write("No File Selected", LogFormat.Error); 
      result = MessageBox.Show(@"Please select a file..", @"No File Selected!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation); 
      if (result == DialogResult.Abort || result == DialogResult.Cancel) { break; } 
      if (result == DialogResult.Retry) { return GetFile(); } 
     } 

     MyLog.Write($"FilePath: {dialog.FileName}", LogFormat.Process); 
     MyLog.Write(@"File Retrieval was Successful", LogFormat.Result); 
     _lastFilePath = Path.GetDirectoryName(dialog.FileName); 
     return dialog.FileName; 
    } 
    return null; 
} 
+0

は、デバッガでそれを置くことがありますか? – DWright

+0

'try'セクションに例外がスローされないため、' catch'文は決して起動しません。 – Aaron

答えて

1

あなたは以来、あなたのケースでのtry-catchを実行する必要はありませんユーザーがファイルダイアログを使用して何も選択していないときに例外がスローされたりキャッチされたりすることはありません。

試してみてください。

while (result == DialogResult.Retry) { 
     if (dialog.ShowDialog() != DialogResult.OK) { 
       MyLog.Write(@"File Retrieval was Unsuccessful", LogFormat.Result); 
       MyLog.Write("No File Selected", LogFormat.Error); 
      result = MessageBox.Show(@"Please select a file..", @"No File Selected!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation); 
       if (result == DialogResult.Abort) throw; 
        return null; 
      } else { 
       MyLog.Write($"FilePath: {dialog.FileName}", LogFormat.Process); 
       MyLog.Write(@"File Retrieval was Successful", LogFormat.Result); 
       _lastFilePath = Path.GetDirectoryName(dialog.FileName); 
       return dialog.FileName; 
      } 
    } 
+0

これを試しました。 catchブロックでコードを実行しているようですが、まだ100%ではありません。 MessageBoxの再試行ボタンに機能を追加するにはどうすればよいですか?それは現在何もしていない.. – TheAuzzieJesus

関連する問題