打开多个文件(OpenFileDialog,C#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1311578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:59:13  来源:igfitidea点击:

Opening multiple files (OpenFileDialog, C#)

c#winformsfileopenfiledialog

提问by jay_t55

I'm trying to open multiple files at once with the OpenFileDialog, using FileNamesinstead of FileName. But I cannot see any examples anywhere on how to accomplish this, not even on MSDN. As far as I can tell - there's no documentation on it either. Has anybody done this before?

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

采纳答案by RRUZ

You must set the OpenFileDialog.MultiselectProperty value to true, and then access the OpenFileDialog.FileNamesproperty.

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

Check this sample

检查这个样本

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

回答by Matin

You can use this method for text files:

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

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