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

时间:2016-08-12 02:04:00

标签: c# dialog openfiledialog

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

我的目标是实现一个MessageBox,它输出一个带有重试和关闭按钮的错误消息,这两个按钮在功能上都有效。

当执行以下代码时,它只是循环try块并且永远不会进入catch块,这是我想要它在我没有选择文件或单击OpenFileDialog上的取消时所要做的。

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

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var ClientSchema = new Schema({
    measurements: [{
        height: Number,
        weight: Number
    }],
    personal_information: [{
        birthday: Date,
        gender: String
    }],
    contact_information: [{
        phone: String,
        email: String
    }],
    progress: [{
        workouts: WorkoutList
    }]
});

我尝试将代码调整为similar question,但我很难理解为什么逻辑在我的上下文中不起作用。

我做错了什么?

修改 使用Ephraim的答案,我能够提出以下似乎有效的方法..

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

1 个答案:

答案 0 :(得分: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;
            }
    }