2016-08-12 28 views
0

試圖編寫一些代碼來檢索文件的文件路徑,以便稍後可以在ReadFile()函數中使用它,但會遇到一些問題。在MessageBox問題上實現重試按鈕功能(C#)

我的目標是實現一個MessageBox,它輸出一個錯誤消息,並帶有一個重試和關閉按鈕,這兩個功能都可以工作。

當執行下面的代碼時,它會簡單地循環try塊,並且永遠不會進入catch塊,這是我不想選擇文件或在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) { 
     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; 
} 

我已經嘗試了代碼適應一個similar question,但我在努力理解爲什麼邏輯是不是在我的環境中工作。

我做錯了什麼?

編輯: 使用以法蓮的回答,我能想出這似乎工作如下..

// 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

'catch'語句永遠不會觸發,因爲'try'部分沒有拋出任何異常。 – 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