File Information: Operations on Files |
|
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles btnOpen.Click Dim rdrMembers As StreamReader = New StreamReader("People.txt") txtFirstName.Text = rdrMembers.ReadLine() txtLastName.Text = rdrMembers.ReadLine() txtHomePhone.Text = rdrMembers.ReadLine() txtEmailAddress.Text = rdrMembers.ReadLine() rdrMembers.Close() End Sub
|
Deleting a File |
If you have an existing file you don't need anymore, you can delete it. This operation can be performed by calling the FileInfo.Delete() method. Its syntax is: Overrides Public Sub Delete() Here is an example: Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click Dim fleMembers As FileInfo = New FileInfo("First.txt") fleMembers.Delete() End Sub |
Copying a File |
You can make a copy of a file from one directory to another. To do this, you can call the FileInfo.CopyTo() method that is overloaded with two versions. The first version has the following syntax: Overloads Public Function CopyTo(ByVal destFileName As String) As FileInfo When calling this method, specify the path or directory that will be the destination of the copied file. Here is an example: Private Sub btnCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCopy.Click Dim fleMembers As FileInfo = New FileInfo("People.txt") Dim strMyDocuments As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) fleMembers.CopyTo(strMyDocuments & "\Persons.txt") End Sub |
In this example, a file named Reality.txt in the directory of the project would be retrieved and its content would be applied to a new file named Federal.txt created in the My Documents folder of the local computer. When calling the first version of the FileInfo.CopyTo() method, if the file exists already, the operation would not continue and you would simply receive a message box. If you insist, you can overwrite the target file. To do this, you can use the second version of this method. Its syntax is: Overloads Public Function CopyTo(ByVal destFileName As String, _ ByVal overwrite As Boolean) As FileInfo The first argument is the same as that of the first version of the method. The second argument specifies what action to take if the file exists already in the target directory. If you want to overwrite it, pass the argument as true; otherwise, pass it as false. |
Moving a File |
If you copy a file from one directory to another, you would have two copies of the same file or the same contents in two files. Instead of copying, if you want, you can simply move the file from one directory to another. This operation can be performed by calling the FileInfo.MoveTo() method. Its syntax is: Public Sub MoveTo(ByVal destFileName As String) The argument to this method is the same as that of the CopyTo() method. After executing this method, the FileInfo object would be moved to the destFileName path. Here is an example: |
Private Sub btnMove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMove.Click Dim fleMembers As FileInfo = New FileInfo("Persons.txt") Dim strMyDocuments As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) fleMembers.CopyTo(strMyDocuments & "\peeps.txt") End Sub
|
||
Previous | Copyright © 2005-2016, FunctionX | Next |
|