C# 使用if/else组合和try/catch组合的区别是什么?我把你的答案和我的答案结合起来。请查看我的答案,看看它对您是否合适。您的解决方案是可以的。但是有一句话。如果没有相应的catch块,则使用try块没有意义。在这种情况下,将在应用程序级别捕获异常。

C# 使用if/else组合和try/catch组合的区别是什么?我把你的答案和我的答案结合起来。请查看我的答案,看看它对您是否合适。您的解决方案是可以的。但是有一句话。如果没有相应的catch块,则使用try块没有意义。在这种情况下,将在应用程序级别捕获异常。,c#,openfiledialog,multi-select,.net,file-browser,C#,Openfiledialog,Multi Select,.net,File Browser,使用if/else组合和try/catch组合的区别是什么?我把你的答案和我的答案结合起来。请查看我的答案,看看它对您是否合适。您的解决方案是可以的。但是有一句话。如果没有相应的catch块,则使用try块没有意义。在这种情况下,将在应用程序级别捕获异常。我更愿意添加相应的catch块,并在其中添加日志消息。if/else和try/catch之间的区别很大。其实他们有不同的感觉。第一个用于算法路径选择,第二个用于独占态势处理。这种情况通常不是算法的一部分,在catch块中,您只捕获一些情况,这些


使用if/else组合和try/catch组合的区别是什么?我把你的答案和我的答案结合起来。请查看我的答案,看看它对您是否合适。您的解决方案是可以的。但是有一句话。如果没有相应的
catch
块,则使用
try
块没有意义。在这种情况下,将在应用程序级别捕获异常。我更愿意添加相应的
catch
块,并在其中添加日志消息。if/else和try/catch之间的区别很大。其实他们有不同的感觉。第一个用于算法路径选择,第二个用于独占态势处理。这种情况通常不是算法的一部分,在catch块中,您只捕获一些情况,这些情况不是在算法中规划的,而是在某些情况下发生的。您希望仅在单击合并按钮时复制文件,而不是在选择了合并按钮后复制文件,因此file.copy操作必须在合并按钮下移动(执行单击方法:)希望仅在单击合并按钮时复制文件,而不是在选择文件后复制文件,因此必须将file.copy操作移到合并按钮下执行单击方法:)
private void Form1_Load(object sender, EventArgs e)
{
  InitializeOpenFileDialog();
}

private void InitializeOpenFileDialog()
{
  // Set the file dialog to filter for graphics files.
  this.openFileDialog1.Filter =
    "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" +
    "All files (*.*)|*.*";

  // Allow the user to select multiple images.
  this.openFileDialog1.Multiselect = true;
  this.openFileDialog1.Title = "My Image Browser";
}

private void selectFilesButton_Click(object sender, EventArgs e)
{
  DialogResult dr = this.openFileDialog1.ShowDialog();
  if (dr == System.Windows.Forms.DialogResult.OK)
  {
    // Read the files
    foreach (String file in openFileDialog1.FileNames) 
    {
        // Create a PictureBox.
        try
        {
            PictureBox pb = new PictureBox();
            Image loadedImage = Image.FromFile(file);
            pb.Height = loadedImage.Height;
            pb.Width = loadedImage.Width;
            pb.Image = loadedImage;
            flowLayoutPanel1.Controls.Add(pb);
        }
        catch (SecurityException ex)
        {
            // The user lacks appropriate permissions to read files, discover paths, etc.
            MessageBox.Show("Security error. Please contact your administrator for details.\n\n" +
                "Error message: " + ex.Message + "\n\n" +
                "Details (send to Support):\n\n" + ex.StackTrace
            );
        }
        catch (Exception ex)
        {
            // Could not load the image - probably related to Windows file system permissions.
            MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\\'))
                + ". You may not have permission to read the file, or " +
                "it may be corrupt.\n\nReported error: " + ex.Message);
        }
    }
}
        OpenFileDialog od = new OpenFileDialog();
        od.Filter = "XLS files|*.xls";
        od.Multiselect = true;
        if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string tempFolder = System.IO.Path.GetTempPath();

            foreach (string fileName in od.FileNames)
            {
                System.IO.File.Copy(fileName, tempFolder + @"\" + System.IO.Path.GetFileName(fileName));
            }
        }
private void sourceFiles_Click(object sender, EventArgs e)
{
    Stream myStream;
    OpenFileDialog sourceFileOpenFileDialog = new OpenFileDialog();

    this.sourceFileOpenFileDialog.InitialDirectory = "i:\\CommissisionReconciliation\\Review\\";
    this.sourceFileOpenFileDialog.Filter = "Excel Files (*.xls;*.xlsx;)|*.xls;*.xlsx;|All Files (*.*)|*.*";
    this.sourceFileOpenFileDialog.FilterIndex = 2;
    this.sourceFileOpenFileDialog.RestoreDirectory = true;
    this.sourceFileOpenFileDialog.Multiselect = true;
    this.sourceFileOpenFileDialog.Title = "Please Select Excel Source File(s) for Consolidation";

    if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
    {
        try
        {
            if ((myStream = sourceFileOpenFileDialog.OpenFile()) != null)
            {
                using (myStream)
                {
                    // populates text box with selected filenames 
                    textBoxSourceFiles.Text = sourceFileOpenFileDialog.FileNames; 
                }
            }       // ends if 
        }           // ends try 

        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
        }
    }              // ends if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
}                  // ends public void sourceFiles_Click

private void consolidateButton_Execute_Click(object sender, EventArgs e)
{

    string consolidatedFolder = targetFolderBrowserDialog.SelectedPath; 

        foreach (String file in sourceFileOpenFileDialog.FileNames)
        {
            try
            {
                // Copy each selected xlsx files into the specified TargetFolder 

                System.IO.File.Copy(sourceFileOpenFileDialog.FileName, consolidatedFolder + @"\" + System.IO.Path.GetFileName(sourceFileOpenFileDialog.FileName));
                Log("File" + sourceFileOpenFileDialog.FileName + " has been copied to " + consolidatedFolder + @"\" + System.IO.Path.GetFileName(sourceFileOpenFileDialog.FileName));
            }  
        }          // ends foreach loop
}                  // ends void consolidateButton_Execute_Click