Use MaxFileSize With Openfile Common Dialogs

Use MaxFileSize With Openfile Common Dialogs

When working with OpenFile common dialogs, set a large value for the MaxFileSize property, which affects the maximum length of the string returned in the FileName property. This is the string containing the names of all the files selected by the user. For example, you can reserve 10 kilobytes for this string by executing this statement before showing the dialog:

 CommonDialog1.MaxFileSize = 10240

The returned string’s format depends on how many files the user selects. If the user selects only one file, the string returns in the FileName property that contains the complete name of this file (path + name). If the user selects multiple files, the returned value contains the directory name, followed by all the names without the path. If you use the cdlOFNExplorer flag, the separator character is the Null character. If you’re using VB6, you can use the Split function to quickly parse the returned value:

 CommonDialog1.Filter = "All files (*.*)|*.*"CommonDialog1.FilterIndex = 1CommonDialog1.Flags = cdlOFNAllowMultiselect Or _	cdlOFNFileMustExist Or cdlOFNExplorerCommonDialog1.MaxFileSize = 10240CommonDialog1.CancelError = TrueCommonDialog1.Filename = ""CommonDialog1.ShowOpenIf Err = 0 Then	' Parse the result into an array of strings	Dim names() As String, i As Integer	names() = Split(CommonDialog1.Filename, vbNullChar)	' Print file names, including path	If UBound(names) = 0 Then		' only one file was selected		Print names(0)	Else		' multiple files were selected		For i = 1 To UBound(names)			Print names(0) & "" & names(i)		Next	End IfEnd If
See also  How to Create and Deploy QR Codes Online: A Comprehensive Guide

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist