You receive a "Too many files selected" error message when you use the OpenFileDialog control (820631)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

SYMPTOMS

You may have a Windows Form with an OpenFileDialog control, and the Multiselect property of the control is set to True. When you run your application, and you try to open more than 200 files, you may receive the following error message:

An unhandled exception of type 'System.InvalidOperationException' occurred in system.windows.forms.dll
Additional information: Too many files selected. Please select fewer files and try again.

CAUSE

This problem occurs because the application uses the Open File common dialog box with the OFN_ALLOWMULTISELECT flag set. The buffer that is passed to the Open File common dialog box in the OPENFILENAME.lpstrFile field is too small. Therefore, the GetOpenFileName function returns an error value, and you receive the error message when you try to open more than 200 files.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Problem

  1. In Microsoft Visual Studio .NET or in Microsoft Visual Studio 2005, create a new Microsoft Windows application. You can use either Microsoft Visual Basic .NET, Microsoft Visual Basic 2005, or Microsoft Visual C# .NET.

    By default, Form1 is created.
  2. From the Toolbox, drag a Button control to Form1.
  3. From the Toolbox, drag an OpenFileDialog control to Form1.
  4. Right-click OpenFileDialog1, and then click Properties.
  5. In the Properties window, set the Multiselect property to True.
  6. Add the following code to the Click event of Button1:

    Visual Basic .NET or Visual Basic 2005 Code
    'Displays the Open dialog box.
    OpenFileDialog1.ShowDialog()
    Visual C# .NET Code
     //Displays the Open dialog box.
    openFileDialog1.ShowDialog();
  7. On the Build menu, click BuildSolution.
  8. On the Debug menu, click Start.
  9. Click Button1.
  10. In the Open dialog box, move to a folder with more than 200 files. Select more than 200 files, and then click Open.
  11. You receive the error message in the "Symptoms" section.

REFERENCES

For more information about the OpenFileDialog class, visit the following Microsoft Web site:

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformsopenfiledialogclasstopic.asp For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

131462 HOWTO: Handle FNERR_BUFFERTOOSMALL in Windows


Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbDlg kbProperties kbCtrl kbControl kbForms kbWindowsForms kbprb KB820631 kbAudDeveloper