Disk I/O: Writing

The "Save a File" button displays a Save File dialog window to let users specify a directory as well as a filename. Once you've selected these two items, you will save the current date and time to the specified file:

'===================================================
    ' Save a File
    '===================================================
    Private Sub btnSaveFile_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles btnSaveFile.Click
        '---Create a SaveFileDialog to request a path and file
        ' name to save to---
        Dim saveFile1 As New SaveFileDialog()
        '---Initialize the SaveFileDialog to specify the txt
        ' extension for the file---
        saveFile1.DefaultExt = "*.txt"
        saveFile1.Filter = "Text Files|*.txt"

        '---Determine if the user selected a file name from the
        ' saveFileDialog---
        If (saveFile1.ShowDialog() = _
            System.Windows.Forms.DialogResult.OK) _
            And (saveFile1.FileName.Length) > 0 Then
            '---Write the current date and time to file---
            My.Computer.FileSystem.WriteAllText( _
               saveFile1.FileName, Now.ToString, False)
            MsgBox("Content written to text file.")
        End If
    End Sub

Get Use ClickOnce to Deploy Windows Applications now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.