Reputation: 11662
I'm trying to open multiple files at once with the OpenFileDialog
, using FileNames
instead 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?
Upvotes: 40
Views: 90676
Reputation: 23
I couldn't get the foreach loop to work using the multiselect = true
on its own for some reason. But the following worked for me instead. Comments inline explaining the code.
OpenFileDialog dialog = new OpenFileDialog(); //Instantiates a new open file dialog
dialog.Multiselect = true; //Allows the user to select multiple files when this is set to true
dialog.Filter = "All files (*.*)|*.*"; //The file filter determines which file types are displayed in the dialog for selection. This can be tailored for any file type
dialog.Title = "Your Title"; //The title that will be displayed at the top of the dialog window when it is open
dialog.CheckFileExists = true; //Will verify if the file being specified exists when set to true
dialog.CheckPathExists = true; //Will verify if the folder path/directory being specified exists when set to true
dialog.InitialDirectory = @"Your Specified Folder Path"; //This is the folder that opens when the dialog is opened
dialog.ShowDialog(); //Show the file selection dialog window
try
{
//Go through the array of files selected from the dialog, starting from the beginning, and open each of the files in that array
for (int i = 0; i < dialog.FileNames.Length; i++) //The ".FileNames" object gets the full file path and file name for all the files that were selected
{
Process.Start(dialog.FileNames[i]); //Open all the selected files
//File.Copy(dialog.FileNames[i].ToString(), loadToFolder + dialog.SafeFileNames[i], true); //Copy all the selected files from one location to another
}
}
catch (Exception ex) //Safely catch any errors and display it as a message box to the user
{
MessageBox.Show("Unable to Add File(s) to List.\n" + ex.Message.ToString(), "File Add Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Upvotes: 1
Reputation: 68
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;
}
}
Upvotes: 1
Reputation: 136441
You must set the OpenFileDialog.Multiselect
Property value to true, and then access the OpenFileDialog.FileNames
property.
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);
}
}
}
Upvotes: 84