Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-使用循环打开对话框_C#_Loops_While Loop_Dialog - Fatal编程技术网

C#-使用循环打开对话框

C#-使用循环打开对话框,c#,loops,while-loop,dialog,C#,Loops,While Loop,Dialog,我正在尝试制作一个从另一个应用程序加载配置文件的程序。 如果文件存在,它将加载该文件并显示一条消息,但如果配置文件无效,它将显示一条错误消息,然后打开一个对话框以加载正确的文件。但是如果用户重新加载了错误的文件,同样的对话框应该会再次出现,但这就是我的代码失败的时候 类似地,如果文件从一开始就不存在,它会显示一个对话框来加载文件,但如果允许取消对话框或再次选择错误的文件,则“我的代码”将失败 我知道解决方案是使用循环,但我不确定如何构造循环。 Pd:searchfile()是打开对话框的函数,r

我正在尝试制作一个从另一个应用程序加载配置文件的程序。 如果文件存在,它将加载该文件并显示一条消息,但如果配置文件无效,它将显示一条错误消息,然后打开一个对话框以加载正确的文件。但是如果用户重新加载了错误的文件,同样的对话框应该会再次出现,但这就是我的代码失败的时候

类似地,如果文件从一开始就不存在,它会显示一个对话框来加载文件,但如果允许取消对话框或再次选择错误的文件,则“我的代码”将失败

我知道解决方案是使用循环,但我不确定如何构造循环。 Pd:searchfile()是打开对话框的函数,readconfig()是读取另一个应用程序的配置文件的函数

    strfilenamepath = @"C:\Users\test\dogs.exe.config";

    if (File.Exists(strfilenamepath))
    {
        onlyFilename = System.IO.Path.GetFileName(strfilenamepath);
        textBox1.Text = onlyFilename;
        try
        {
            string[] valores = readConfig(strfilenamepath);
            MessageBox.Show(valores[0] + valores[1] + valores[2]);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error loading config file." + ex.Message);
            searchFile();
            onlyFilename = System.IO.Path.GetFileName(strfilenamepath);
            textBox1.Text = onlyFilename;
            string[] valores = readConfig(strfilenamepath);
            MessageBox.Show(valores[0] + valores[1] + valores[2]);
        }
    }
    else
    {
        searchFile();
        onlyFilename = System.IO.Path.GetFileName(strfilenamepath);
        textBox1.Text = onlyFilename;
        try
        {
            readConfig(strfilenamepath);
            string[] valores = readConfig(strfilenamepath);
            MessageBox.Show(valores[0] + valores[1] + valores[2]);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error loading config file." + ex.Message);
            searchFile();
            onlyFilename = System.IO.Path.GetFileName(strfilenamepath);
            textBox1.Text = onlyFilename;
            string[] valores = readConfig(strfilenamepath);
            MessageBox.Show(valores[0] + valores[1] + valores[2]);
        }
    }

您可以这样做:

try
{
    //Code to try open the file to memory
}
catch (Exception ex)
{
    while (true)
    {
        MessageBox.Show(@"Select an valid file");

        var path = searchFile();
        if (string.IsNullOrWhiteSpace(path))
            continue;

        try
        {
            //Code to try open the file to memory
        }
        catch (Exception ex2)
        {
            MessageBox.Show(@"The selected file is not valid");
            continue;
        }

        break;
    }
}

如果您将读取逻辑提取到另一个处理异常的方法,并返回一个布尔值来表示成功和计算结果,那么设计它就更容易了。
TryDoSomething
模式正是这样做的

伪码

public bool TryReadConfig(string path, out string[] valores)
{
    valores = null;
    try {
        valores = read the values;
        return true;
    } catch {
        Display message;
        return false;
    }
}
strfilenamepath = @"C:\Users\test\dogs.exe.config";

while (true) {
    if (File.Exists(strfilenamepath) && TryReadConfig(strfilenamepath, out var valores)) {
        Do something with the valores;
        break;
    }
    var ofd = new OpenFileDialog{ ... };
    if (ofd.ShowDialog() == DialogResult.OK) {
        strfilenamepath = ofd.Filename;
    } else {
        break; // The user canceled the operation.
    }
}
伪代码中的主循环

public bool TryReadConfig(string path, out string[] valores)
{
    valores = null;
    try {
        valores = read the values;
        return true;
    } catch {
        Display message;
        return false;
    }
}
strfilenamepath = @"C:\Users\test\dogs.exe.config";

while (true) {
    if (File.Exists(strfilenamepath) && TryReadConfig(strfilenamepath, out var valores)) {
        Do something with the valores;
        break;
    }
    var ofd = new OpenFileDialog{ ... };
    if (ofd.ShowDialog() == DialogResult.OK) {
        strfilenamepath = ofd.Filename;
    } else {
        break; // The user canceled the operation.
    }
}

环路在哪里?什么是
searchFile()
?我没有使用循环,因为我不知道如何在代码中构造它。Searchfile()只是一个simole函数,可以打开对话框并将路径保存在变量中。您不需要循环。只需向用户发送无效消息,并让用户单击按钮重试。