是否有必要移动OpenFileDialog代码来打开文件并将其放入单独的方法中?

时间:2013-05-25 16:52:12

标签: c# winforms

我想将Add button click event中的代码与代码分开,以使我的代码看起来更加安排。 这是我在这些更改之前的代码(我使用BackgroundWorker是为了避免我的GUI冻结,因为我选择的每个文件都需要打开进程并检查这个文件是否正常,然后再将此文件添加到我的列表框中):

private void btnAddfiles_Click(object sender, EventArgs e)
{
    System.IO.Stream stream;

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        fileSelected();
        if (openFileDialog1.FileNames.Length > 0)
            lastPath = Path.GetDirectoryName(openFileDialog1.FileNames[0]);

        ListboxFile lbf = new ListboxFile();
        lbf.OnFileAddEvent += lbf_OnFileAddEvent;

        BackgroundWorker backgroundWorker = new BackgroundWorker();
        backgroundWorker.WorkerReportsProgress = true;
        backgroundWorker.DoWork +=
        (s3, e3) =>
        {
            foreach (String file in openFileDialog1.FileNames)
            {
                try
                {
                    if ((stream = openFileDialog1.OpenFile()) != null)
                    {
                        int numberOfFiles = openFileDialog1.SafeFileNames.Length;
                        using (stream)
                        {
                            lbf.checkFile(file);
                            lastPath = Path.GetDirectoryName(openFileDialog1.FileNames[0]);
                        }
                    }
                }

                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        };

        backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
            (s3, e3) =>
            {
                //update my gui
            });

        backgroundWorker.RunWorkerAsync();
    }
}

这是在更改之后:

private void btnAddfiles_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        fileSelected();
    }
}

private void fileSelected()
{
    if (openFileDialog1.FileNames.Length > 0)
        lastPath = Path.GetDirectoryName(openFileDialog1.FileNames[0]);

    ListboxFile lbf = new ListboxFile();
    lbf.OnFileAddEvent += lbf_OnFileAddEvent;

    BackgroundWorker backgroundWorker = new BackgroundWorker();
    backgroundWorker.WorkerReportsProgress = true;
    backgroundWorker.DoWork +=
    (s3, e3) =>
    {
        foreach (String file in openFileDialog1.FileNames)
        {
            System.IO.Stream stream;
            try
            {
                if ((stream = openFileDialog1.OpenFile()) != null)
                {
                    int numberOfFiles = openFileDialog1.SafeFileNames.Length;
                    using (stream)
                    {
                        lbf.checkFile(file);
                        lastPath = Path.GetDirectoryName(openFileDialog1.FileNames[0]);
                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }
    };

    backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
        (s3, e3) =>
        {
            //update my gui
        });

    backgroundWorker.RunWorkerAsync();
}

那么,我做了什么是必要的? 也许其他更好的东西?

1 个答案:

答案 0 :(得分:0)

在我看来,只需添加更多行代码就可以降低可读性。

我建议你:

  1. 处理点击事件处理程序中的所有OpenFileDialog相关代码

  2. 将选定文件列表传递给名为CheckAndShowFiles(IEnumerable<string> files)的方法。

  3. 让CheckFile返回一个bool而不是抛出异常。

  4. 检查文件并添加到一步foreach文件中查看,以便为用户提供进度感。