【问题标题】:Opening multiple files (OpenFileDialog, C#)打开多个文件 (OpenFileDialog, C#)
【发布时间】:2010-11-21 15:30:56
【问题描述】:

我正在尝试使用OpenFileDialog 一次打开多个文件,使用FileNames 而不是FileName。但是我在任何地方都看不到任何关于如何做到这一点的例子,甚至在 MSDN 上也看不到。据我所知 - 也没有关于它的文档。以前有人做过吗?

【问题讨论】:

    标签: c# winforms file openfiledialog


    【解决方案1】:

    您可以将此方法用于文本文件:

    OpenFileDialog open = new OpenFileDialog();
    open.Filter = "All Files *.txt | *.txt";
    open.Multiselect = true;
    open.Title = "Open Text Files";
    
    if (open.ShowDialog() == DialogResult.OK)
    {
        foreach (String file in open.FileNames)
        {    
             string temp = YourRichTextBox.Text;
    
             YourRichTextBox.LoadFile(file, RichTextBoxStreamType.PlainText);
    
             YourRichTextBox.Text += temp;
        }
    }
    

    【讨论】:

      【解决方案2】:

      您必须将OpenFileDialog.Multiselect 属性值设置为true,然后访问OpenFileDialog.FileNames 属性。

      检查此示例

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多