【问题标题】:C# - Using loop to open dialog boxC# - 使用循环打开对话框
【发布时间】:2019-03-07 22:29:21
【问题描述】:

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

同样,如果文件从一开始就不存在,它会显示一个对话框来加载文件,但是如果给出取消对话框或再次选择错误的文件,我的代码就会失败。

我知道解决方案是使用循环,但我不确定如何构建它。 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]);
        }
    }

【问题讨论】:

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

标签: c# loops while-loop dialog


【解决方案1】:

如果您将读取逻辑提取到另一个处理异常并返回布尔值以表示成功和计算结果的方法,则设计它会更容易。 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.
    }
}

【讨论】:

    【解决方案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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多