在 MessageBox 问题上实现重试按钮功能 (C#)

Implementing Retry Button Functionality on MessageBox Issues (C#)

正在尝试编写一些代码来检索文件的文件路径,以便以后可以在 ReadFile() 函数中使用它,但是 运行 遇到了一些问题。

我的目标是实现一个 MessageBox,它输出一条错误消息,带有重试和关闭按钮,两者都可以正常工作。

当执行下面的代码时,它只是简单地循环 try 块,从不进入 catch 块,这是我在没有 select 文件或单击取消时希望它做的打开文件对话框。

下面是我目前使用的代码..

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,但我很难理解为什么逻辑在我的上下文中不起作用。

我做错了什么?

编辑: 使用 Ephraim 的回答,我能够想出以下似乎有效的方法..

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

您不需要在您的案例中执行 try-catch,因为当用户不使用文件对话框时 select 不会抛出或捕获任何异常。

尝试:

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;
            }
    }