Dim fileLoc As String = "c:\sample1.txt"
Create a Text File
' Create a Text File
Private Sub btnCreate_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim fs As FileStream = Nothing
If (Not File.Exists(fileLoc)) Then
fs = File.Create(fileLoc)
Using fs
End Using
End If
End Sub
Write to a Text File
' Write to a Text File
Private Sub btnWrite_Click(ByVal sender As Object, ByVal e As EventArgs)
If File.Exists(fileLoc) Then
Using sw As StreamWriter = New StreamWriter(fileLoc)
sw.Write("Some sample text for the file")
End Using
End If
End Sub
Read From a Text File
' Read From a Text File
Private Sub btnRead_Click(ByVal sender As Object, ByVal e As EventArgs)
If File.Exists(fileLoc) Then
Using tr As TextReader = New StreamReader(fileLoc)
MessageBox.Show(tr.ReadLine())
End Using
End If
End Sub
Copy a Text File
' Copy a Text File
Private Sub btnCopy_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim fileLocCopy As String = "d:\sample1.txt"
If File.Exists(fileLoc) Then
' If file already exists in destination, delete it.
If File.Exists(fileLocCopy) Then
File.Delete(fileLocCopy)
End If
File.Copy(fileLoc, fileLocCopy)
End If
End Sub
Move a Text File
' Move a Text file
Private Sub btnMove_Click(ByVal sender As Object, ByVal e As EventArgs)
' Create unique file name
Dim fileLocMove As String = "d:\sample1" & System.DateTime.Now.Ticks & ".txt"
If File.Exists(fileLoc) Then
File.Move(fileLoc, fileLocMove)
End If
End Sub
Delete a Text File
' Delete a text file
Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
If File.Exists(fileLoc) Then
File.Delete(fileLoc)
End If
End Sub
No comments:
Post a Comment