File Information |
|
Introduction |
In its high level of support for file processing, the .NET Framework provides the FileInfo class. This class is equipped to handle all types of file-related operations including creating, copying, moving, renaming, or deleting a file. FileInfo is based on the FileSystemInfo class that provides information on characteristics of a file. To assist you with finding information about a file, the FileSystem class from the My object is equipped with a method named GetFileInfo. |
The FileInfo class is equipped with one constructor whose syntax is: Public Sub New(fileName As String) This constructor takes as argument the name of a file or its complete path. If you provide only the name of the file, the compiler would consider the same directory of its project. As mentioned previously, to get information about a file, you can call the GetFileInfo() method of the FileSystem class from the My object. Its syntax is: Public Shared Function GetFileInfo(file As String) As FileInfo This shared method returns a FileInfo object. Here is an example of calling it: Imports System.IO Public Class Exercise Private Filename As String Private Sub btnFileInformation_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnFileInformation.Click Dim PeopleInformation As FileInfo PeopleInformation = My.Computer.FileSystem.GetFileInfo(Filename) End Sub End Class After calling this method, you can then use its returned value to get the information you want about the file.
The FileInfo constructor is mostly meant only to indicate that you want to use a file, whether it exists already or it would be created. Based on this, if you execute an application that has only a FileInfo object created using the constructor as done above, nothing would happen. To create a file, you have various alternatives. If you want to create one without writing anything in it, which implies creating an empty file, you can call the FileInfo.Create() method. Its syntax is: Public Function Create As FileStream This method simply creates an empty file. Here is an example of calling it: Private Sub btnFileInformation_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnFileInformation.Click Dim PeopleInfo As FileInfo = New FileInfo("People.txt") PeopleInfo.Create() End Sub The FileInfo.Create() method returns a FileStream object. You can use this returned value to write any type of value into the file, including text. If you want to create a file that contains text, an alternative is to call the FileInfo.CreateText() method. Its syntax is: Public Function CreateText As StreamWriter This method returns a StreamWriter object. You can use this returned object to write text to the file. When you call the FileInfo.Create() or the FileInfo.CreateText() method, if the file passed as argument, or as the file in the path of the argument, exists already, it would be deleted and a new one would be created with the same name. This can cause an important file to be deleted. Therefore, before creating a file, you may need to check whether it exists already. To do this, you can check the value of the Boolean FileInfo.Exists property. This property holds a True value if the file exists already and it holds a False value if the file does not yet exist or it does not exist in the path. Here is an example of calling it: Private Sub btnFileInformation_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnFileInformation.Click Dim Filename As String Dim PeopleInformation As FileInfo Filename = "Student12.std" PeopleInformation = My.Computer.FileSystem.GetFileInfo(Filename) If PeopleInformation.Exists = True Then MsgBox("The file exists already") Else MsgBox("Unknown file") End If End Sub
As mentioned earlier, the FileInfo.Create() and the FileInfo.CreateText() methods can be used to create a file but they not write values to the file. To write values in the file, each method returns an appropriate object. The FileInfo.Create() method returns FileStream object. You can use this to specify the type of operation that would be allowed on the file. To write normal text to a file, you can first call the FileInfo.CreateText() method that returns a StreamWriter object. Here is an example: Private Sub btnSave_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnSave.Click Dim Filename As String Dim StudentsWriter As StreamWriter Dim StudentInformation As FileInfo Filename = "Student1.std" StudentInformation = My.Computer.FileSystem.GetFileInfo(Filename) StudentsWriter = StudentInformation.CreateText() Try StudentsWriter.WriteLine(txtFirstName.Text) StudentsWriter.WriteLine(txtLastName.Text) StudentsWriter.WriteLine(cbxGenders.SelectedIndex) txtFirstName.Text = "" txtLastName.Text = "" cbxGenders.SelectedIndex = 2 Finally StudentsWriter.Close() End Try End Sub As an alternative to Create() or CreateText(), if you want to create a file that can only be written to, you can call the FileInfo.OpenWrite() method. Its syntax is: Public Function OpenWrite As FileStream This method returns a FileStream that you can then use to write values into the file.
You may have created a text-based file and written to it. If you open such a file and find out that a piece of information is missing, you can add that information to the end of the file. To do this, you can call the FileInfo.AppenText() method. Its syntax is: Public Function AppendText As StreamWriter When calling this method, you can retrieve the StreamWriter object that it returns, then use that object to add new information to the file.
As opposed to writing to a file, you can read from it. To support this, the FileInfo class is equipped with a method named OpenText(). Its syntax is: Public Function OpenText As StreamReader This method returns a StreamReader object. You can then use this object to read the lines of a text file. Here is an example: Private Sub btnOpen_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnOpen.Click Dim Filename As String Dim StudentsReader As StreamReader Dim StudentInformation As FileInfo Filename = "Student1.std" StudentInformation = My.Computer.FileSystem.GetFileInfo(Filename) StudentsReader = StudentInformation.OpenText Try txtFirstName.Text = StudentsReader.ReadLine txtLastName.Text = StudentsReader.ReadLine cbxGenders.SelectedIndex = CInt(StudentsReader.ReadLine) Finally StudentsReader.Close() End Try End Sub If you want to open a file that can only be read from, you can call the FileInfo.OpenRead() method. Its syntax is: Public Function OpenRead As FileStream This method returns a FileStream that you can then use to read values from the file.
As opposed to creating a file, probably the second most regular operation performed on a file consists of opening it to read or explore its contents. To support opening a file, the FileInfo class is equipped with the Open() method that is overloaded with three versions. Their syntaxes are: Public Function Open ( _ mode As FileMode _ ) As FileStream Public Function Open ( _ mode As FileMode, _ access As FileAccess _ ) As FileStream Public Function Open ( _ mode As FileMode, _ access As FileAccess, _ share As FileShare _ ) As FileStream You can select one of these methods, depending on how you want to open the file, using the options for file mode, file access, and file sharing. Each version of this method returns a FileStream object that you can then use to process the file. After opening the file, you can then read or use its content.
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: Public Overrides Sub Delete Here is an example: Private Sub btnDelete_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnDelete.Click Dim Filename As String Dim StudentInformation As FileInfo Filename = "Student1.std" StudentInformation = My.Computer.FileSystem.GetFileInfo(Filename) If PeopleInformation.Exists = True Then StudentInformation.Delete() Else MsgBox("Unknown file") End If End Sub 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. One of the versions has the following syntax: public FileInfo CopyTo(string destFileName) 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 System.Object, _ ByVal e As System.EventArgs) _ Handles btnCopy.Click Dim Filename As String Dim StudentInformation As FileInfo Dim MyDocuments As String = _ Environment.GetFolderPath(Environment.SpecialFolder.Personal) Filename = "Student1.std" StudentInformation = My.Computer.FileSystem.GetFileInfo(Filename) If StudentInformation.Exists = True Then StudentInformation.CopyTo(MyDocuments & "\Federal.txt") Else MsgBox("Unknown file") End If 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 current user. 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: Public Function CopyTo(destFileName As String, 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 second argument as true; otherwise, pass it as false. Here is an example: Private Sub btnCopy_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnCopy.Click Dim Filename As String Dim StudentInformation As FileInfo Dim MyDocuments As String = _ Environment.GetFolderPath(Environment.SpecialFolder.Personal) Filename = "Student1.std" StudentInformation = My.Computer.FileSystem.GetFileInfo(Filename) If StudentInformation.Exists = True Then StudentInformation.CopyTo(MyDocuments & "\Federal.txt", True) Else MsgBox("Unknown file") End If End Sub 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 a file from one directory to another. This operation can be performed by calling the FileInfo.MoveTo() method. Its syntax is: Public Sub MoveTo(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 System.Object, _ ByVal e As System.EventArgs) _ Handles btnMove.Click Dim Filename As String Dim StudentInformation As FileInfo Dim MyDocuments As String = _ Environment.GetFolderPath(Environment.SpecialFolder.Personal) Filename = "Student1.std" StudentInformation = My.Computer.FileSystem.GetFileInfo(Filename) If StudentInformation.Exists = True Then StudentInformation.MoveTo(MyDocuments & "\Federal.txt") Else MsgBox("Unknown file") End If End Sub
|
|
||
Home | Copyright © 2008-2016, FunctionX, Inc. | |
|