Open and Save File Dialog (VB.Net)
February 25, 2011 Leave a comment
OpenFileDialog and SaveFileDialog are both Microsoft .Net components. You can see them on the toolbox under the tab Dialogs. The main purpose of this pair of dialogs is to facilitate the programmer to add the visual interface for program user to open and save files on local disk or network.
Open File Dialog
Save File Dialog
From the above pictures the difference between the visual interfaces is very much clear, but there is not much difference in the use. The below example will show you the usage of each dialog as we can use it generally in our application.
Private IsFileReadOnly As Boolean
Here code defines the flag for file status that is file is opened in read only mode or editable mode. This flag could be used in the save or edit options to verify that whether the file is read only or editable. In the later code you will see how it is used.
Dim OpenFile As New OpenFileDialog 'displays the text as title of dialog OpenFile.Title = "Open Text File" 'Implementing filter on open file dialog OpenFile.Filter = "TextFile (*.txt;*.rtf)|*.txt;*.rtf|Batch File (*.bat)|*.bat|All Files (*.*)|*.*" 'Setting the initial directory for your datafile OpenFile.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) 'Enabling the option for readonly check box on file dialog OpenFile.ShowReadOnly = True If OpenFile.ShowDialog() = Windows.Forms.DialogResult.OK Then 'checking the the read only option If OpenFile.ReadOnlyChecked = True Then MsgBox("You are opening the file as read only. You would not be able to save changes") IsFileReadOnly = OpenFile.ReadOnlyChecked 'setting global option for read only End If 'procedure to read text file into text box ReadTextFile(OpenFile.FileName) 'displays the file name on form title bar Me.Text = OpenFile.SafeFileName End If OpenFile.Dispose()
The above code is the part of the application where user clicks the menu or button for opening the existing file. In code we have used some properties to set the initial behaviour of the OpenFileDialog.
The property Title sets the title text, which appears on the title bar of the dialog. You can sent any value but it is preferable to give such title that describes the type or use of file Intended for open.
The Filter is an other significant property, which is frequently used in application. It limits the user for the type of files that application can support. The filter string contains a description of the filter, followed by the vertical bar (|) and the filter pattern. The strings for different filtering options are separated by the vertical bar. You can add several filter patterns to a filter by separating the file types with semicolons.
” TextFile (*.txt;*.rtf)|*.txt;*.rtf|Batch File (*.bat)|*.bat|All Files (*.*)|*.* “
The InitialDirectory is used to initially point the location, where application is configured to store its data files, documents, pictures, etc. it is not necessary to use this property but it is a best practice to take the application user at the location which is marked by the application for storing its custom type files. Anyhow user is not bound, he/she may go to any location by navigating through the interface.
ShowReadOnly Property, enables or disable the check box on visual interface. This check box is used to get input from user to specify that the file, he is going to open is read only. The status of this check box can be retrieve through an other property ReadOnlyChecked, you can set the value in some global flag that you can use later in your application, as described above.
SafeFileName property returns the file name along with its extension without path.
Below is source for opening the text file, will be discuss in some other blog in detail.
'Procedure to read the text file from the harddisk Dim fileContents As String fileContents = My.Computer.FileSystem.ReadAllText(FName) TextBox1.Text = fileContents
The usage of SaveFileDialog is almost the same except for few properties, which I explained below.
If IsFileReadOnly = True Then MsgBox("The file is read only.") Exit Sub End If Dim SaveFile As New SaveFileDialog 'displays the text as title of dialog SaveFile.Title = "Save Text File As" SaveFile.DefaultExt = "txt" 'Implementing filter on open file dialog SaveFile.Filter = "TextFile (*.txt;*.rtf)|*.txt;*.rtf|Batch File (*.bat)|*.bat|All Files (*.*)|*.*" 'Setting the initial directory for your datafile SaveFile.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) SaveFile.OverwritePrompt = True If SaveFile.ShowDialog = Windows.Forms.DialogResult.OK Then 'call save file procedure SaveTextFile(SaveFile.FileName) 'displays the file name on form title bar Me.Text = System.IO.Path.GetFileName(SaveFile.FileName) MsgBox("File has Saved") End If SaveFile.Dispose()
In the above code I have used the global Flag IsFileReadOnly, which I used here to restrict the application to save the read only file. It prompts the user that file is read only and you can not save file.
In this code I used the DefaultExt property to specify the file extension, when user is saving the file without giving the extension, this default extension is used.
OverwritePrompt property, prompts the user that he/she is going to overwrite the existing file, with the option of yes and no. Here user can decide to overwrite or not.
The below code is used to save the text file on the given location.
My.Computer.FileSystem.WriteAllText(FName, TextBox1.Text, False)
I hope this would be quite informative and help full to you. Please don’t forget to leave comments.
Have Fun !