C# Windows 窗体 - 使用 MultiSelect、OpenFileDialog 和 & 选择和复制多个文件文件浏览器对话框

发布于 2024-10-22 02:00:53 字数 193 浏览 6 评论 0原文

我正在使用带有 OpenFileDialog 和 FileBrowserDialog 的 C# 开发 WinForms 应用程序,并且我想:

  1. 启用多个 xls 文件的选择。
  2. 选择后,在文本框中显示选定的 xlsx 文件名
  3. 将选定的文件复制到单独的目录 合并

我该如何完成此操作?

I'm developing a WinForms application using C# with an OpenFileDialog and FileBrowserDialog and I'd like to:

  1. Enable selection of multiple xls files.
  2. After selection is made, Display selected xlsx filenames in textbox
  3. Copy the selected files to a separate directory Consolidated

How can I accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷情 2024-10-29 02:00:53

这是示例代码:

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

Here is sample code:

        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));
            }
        }
洛阳烟雨空心柳 2024-10-29 02:00:53

OpenFileDialog 有一个 MultiSelect 属性,您需要将其设置为 true 以允许选择多个文件。

这里是来自 MSDN 的代码示例它允许用户选择多个图像并将它们显示在窗体上的 PictureBox 控件中:

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

There is MultiSelect property of OpenFileDialog which you need to set to true to allow selecting multiple files.

Here is a code example from MSDN which allows the user to select a multiple number of images and display them in PictureBox controls on a Form:

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);
        }
    }
}
少钕鈤記 2024-10-29 02:00:53

结合这两个答案,这是我想出的代码:

  • 使用户能够选择多个 XLSX 文件(使用 MultiSelect、OpenFileDialog、this.OpenFileDialog Properties 和 FileBrowserDialog)
  • 选择后,在文本框中显示选定的 XLSX 文件名(通过将 textBoxSourceFiles.Text 值设置为 sourceFileOpenFileDialog.FileNames)
  • 将选定的文件复制到单独的合并目录(使用 foreach 循环、System.IO. File.Copy、System.IO.Path.GetFileName、sourceFileOpenFileDialog.FileName)

    private void sourceFiles_Click(对象发送者,EventArgs e)
    {
        流式传输 myStream;
        OpenFileDialog sourceFileOpenFileDialog = new OpenFileDialog();
    
        this.sourceFileOpenFileDialog.InitialDirectory = "i:\\CommissionReconciliation\\Review\\";
        this.sourceFileOpenFileDialog.Filter = "Excel 文件 (*.xls;*.xlsx;)|*.xls;*.xlsx;|所有文件 (*.*)|*.*";
        this.sourceFileOpenFileDialog.FilterIndex = 2;
        this.sourceFileOpenFileDialog.RestoreDirectory = true;
        this.sourceFileOpenFileDialog.Multiselect = true;
        this.sourceFileOpenFileDialog.Title = "请选择要合并的 Excel 源文件";
    
        if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
        {
            尝试
            {
                if ((myStream = sourceFileOpenFileDialog.OpenFile()) != null)
                {
                    使用(myStream)
                    {
                        // 用选定的文件名填充文本框 
                        textBoxSourceFiles.Text = sourceFileOpenFileDialog.FileNames; 
                    }
                } // 结束如果 
            } // 结束尝试 
    
            catch(异常前)
            {
                MessageBox.Show("错误:无法从磁盘读取文件。原始错误:" + ex.Message);
            }
        } // 结束 if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
    } // 结束 public void sourceFiles_Click
    
    私有无效 consolidateButton_Execute_Click(对象发送者,EventArgs e)
    {
    
        字符串solidifiedFolder = targetFolderBrowserDialog.SelectedPath; 
    
            foreach(sourceFileOpenFileDialog.FileNames 中的字符串文件)
            {
                尝试
                {
                    // 将每个选定的 xlsx 文件复制到指定的 TargetFolder 中 
    
                    System.IO.File.Copy(sourceFileOpenFileDialog.FileName,consolidatedFolder + @“\”+ System.IO.Path.GetFileName(sourceFileOpenFileDialog.FileName));
                    Log("File" + sourceFileOpenFileDialog.FileName + " 已复制到 " + consolidatedFolder + @"\" + System.IO.Path.GetFileName(sourceFileOpenFileDialog.FileName));
                }  
            } // 结束 foreach 循环
    } // 结束 void consolidateButton_Execute_Click
    

Combining both answers, here's the code I came up with to:

  • Enabling the user to Select Multiple XLSX Files (using MultiSelect, OpenFileDialog, this.OpenFileDialog Properties & FileBrowserDialog)
  • After selection is made, Display Selected XLSX Filenames in Textbox (by setting textBoxSourceFiles.Text value to sourceFileOpenFileDialog.FileNames)
  • Copy the Selected Files to a separate Consolidated directory (using foreach loop, System.IO.File.Copy, System.IO.Path.GetFileName, sourceFileOpenFileDialog.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
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文