OpenFileDialog Class


OpenFileDialog Class

Namespace

System.Windows.Forms

Creatable

Yes

Description

The OpenFileDialog class represents a common dialog box for selecting or opening a file. The OpenFileDialog class has properties that let you configure, display, and retrieve the results from this dialog box, from which the user selects a single file or multiple files. The dialog box does not open the file(s); it only indicates the file(s) to be opened.

The following list discusses the more interesting members of the OpenFileDialog class.


AddExtension Property

Indicates whether the dialog box should automatically add the default file extension to the user-supplied file name. The default value is true.


CheckFileExists Property

Indicates whether a warning message should be displayed if the user enters the name of a file that does not exist. The default value is true.


DefaultExt Property

Defines the default file extension. The string should consist of the file extension only, without a leading period.


FileName Property

Returns the fully qualified file namethe file name with its complete path and extension. If no file is selected, the property returns an empty string.


FileNames Property

Returns an array of fully qualified file names (with complete paths and extensions) for the files selected by the user. If no file is selected, the property returns an empty array. This property returns a single-element array if the Multiselect property is False and the user selects a file.


Filter Property

Used to configure a filter string that indicates the types of files (by extension) to display in the dialog box. These types appear in the "Files of type" drop-down control on the dialog box. A single item consists of a file description, a vertical bar, and the file extension (usually "*." plus the file extension). If there are multiple extensions in a single item, they are separated by semicolons. If there are multiple items, they are separated by vertical bars. For example, the following code fragment assigns a filter string for text files and VB source code files:

     Dim openPrompt As New OpenFileDialog     openPrompt.Filter = _        "Text files (*.txt; *.vb)|*.txt;*.vb|" & _        "Visual Basic files (*.vb)|*.vb|" & _        "All files (*.*)|*.*" 


FilterIndex Property

Indicates the selected 1-based position in the Filter property's item list. The default is 1. If the user selects a different filter, this property is updated to reflect that change.


InitialDirectory Property

Indicates the initial directory to use when first displaying the dialog.


Multiselect Property

Indicates whether the user is allowed to select more than one file.


OpenFile Method

Opens the file selected by the user, returning a Stream object. The file is opened in read-only mode.


ReadOnlyChecked Property

Indicates whether the Read Only checkbox is selected on the dialog box. The default is False.


RestoreDirectory Property

Indicates whether the current working (default) directory is restored before the dialog box closes. The default value is False.


ShowDialog Method

Displays the dialog box to the user. Once the user has dismissed the dialog box, the FileName and FileNames properties can be used to get the user's selection(s).


ShowReadOnly Property

Indicates whether the Read Only checkbox should appear on the dialog box.


Title Property

Sets the title of the dialog box.

Example

The following code asks the user for one or more files and then displays the filenames in the Output window.

     Dim selectFile As New OpenFileDialog     Dim counter As Integer     selectFile.Multiselect = True     If (selectFile.ShowDialog(  ) = DialogResult.OK) Then        For counter = 0 To UBound(selectFile.FileNames)           Console.WriteLine(selectFile.FileNames(counter))        Next counter     End If 

Version Differences

The public interfaces used for this OpenFileDialog class and the related VB 6 CommonDialog control are quite different.

See Also

SaveFileDialog Class




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net