Microsoft KB Archive/820631

= You receive a &quot;Too many files selected&quot; error message when you use the OpenFileDialog control =

Article ID: 820631

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





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.



Steps to Reproduce the Problem
 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. From the Toolbox, drag a Button control to Form1. From the Toolbox, drag an OpenFileDialog control to Form1. Right-click OpenFileDialog1, and then click Properties. In the Properties window, set the Multiselect property to True.  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; </li> On the Build menu, click BuildSolution.</li> On the Debug menu, click Start.</li> Click Button1.</li> In the Open dialog box, move to a folder with more than 200 files. Select more than 200 files, and then click Open.</li> You receive the error message in the &quot;Symptoms&quot; section.</li></ol>

<div class="references_section">