Home

File Information: Characteristics of a File

 

The Date and Time a File Was Created 

To keep track of it, after a file has been created, the operating system makes a note of the date and the time the file was created. This information can be valuable in other operations such as search routines. You too are allowed to change this date and time values to those you prefer.

As mentioned already, the OS makes sure to keep track of the date and time a file was created. To find out what those date and time values are, you can access the FileSystemInfo.get_CreationTime() property is. This would be done as follows:

Private Sub btnDateTimeCreated_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
			Handles btnDateTimeCreated.Click
        Dim fleMembers As FileInfo = New FileInfo("Persons.txt")
        Dim dteCreationTime As DateTime = fleMembers.CreationTime
        MsgBox("Date and Time Created: " + dteCreationTime.ToString())
End Sub

Of course, by entering the appropriate format in the parentheses of the ToString() method, you can get only either the date or only the time.

If you don't like the date, the time, or both, that the OS would have set when the file was created, you can change them. To change one or both of these values, you can assign a desired DateTime object to the FileSystemInfo.set_CreationTime() property.

 

The Date and Time a File Was Last Accessed 

Many applications allow a user to open an existing file and to modify it. When people work in a team or when a particular file is regularly opened, at one particular time, you may want to know the date and time that the file was last accessed. To get this information, you can access the FileSystemInfo.LastAccessTime property.

If you are interested in know the last date and time a file was modified, you can get the value of its FileSystemInfo.LastWriteTime property. You can also change this value if you want to make sure the file holds your own.

 

The Name of a File

The operating system requires that each file have a name. In fact, the name must be specified when creating a file. This allows the OS to catalogue the computer files. This also allows you to locate or identify a particular file you need.

When reviewing or opening a file, to get its name, the FileInfo class is equipped with the Name property. Here is an example:

MsgBox("The name of this file is: \"" + fleLoan.Name + "\"");

This string simply identifies a file.

 

Practical Learning Practical Learning: Identifying a File

  1. To display the name of the file in the title bar when the user opens it, change the Click event of the Open menu item as follows:
     
    Private Sub mnuFileOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click
            If openFileDialog1.ShowDialog() = DialogResult.OK Then
                ' Create a FileInfo object based on the file
                Dim fleLoan As FileInfo = New FileInfo(openFileDialog1.FileName)
                ' Open the file
                Dim stmLoan As StreamReader = fleLoan.OpenText()
    
                Text = "Loan Preparation: " & fleLoan.Name
    
                ' Use exception handling just in case
                Try
                    txtEmployeeNumber.Text = stmLoan.ReadLine()
                    txtEmployeeName.Text = stmLoan.ReadLine()
                    txtFullName.Text = stmLoan.ReadLine()
                    txtEmployerName.Text = stmLoan.ReadLine()
                    txtHomePhone.Text = stmLoan.ReadLine()
                    txtWorkPhone.Text = stmLoan.ReadLine()
                    txtLoanAmount.Text = stmLoan.ReadLine()
                    txtInterestRate.Text = stmLoan.ReadLine()
                    txtPeriods.Text = stmLoan.ReadLine()
                    txtPayment.Text = stmLoan.ReadLine()
                Finally
                    stmLoan.Close()
                End Try
            End If
    End Sub
  2. Execute the application and try opening a file. Here is an example:
     
  3. Close the form(s) and return to your programming environment
 

The Extension of a File

With the advent of Windows 95 and later, the user doesn't have to specify the extension of a file when creating it. Because of the type of confusions that this can lead to, most applications assist the user with this detail. For example, when we implemented the routines that allow the user to save or open a file, we specified a default extension for the Save Dialog or the Open Dialog objects. This allows the user not to care for the extension. Based on this, some applications allow the user to choose among various extensions. For example, using Notepad, a user can open a text, a PHP, a script, or an HTML file.

When you access a file or when the user opens one, to know the extension of the file, you can access the value of the FileSystemInfo.Extension property. Here is an example:

MsgBox("File Extension: " & fleLoan.Extension);
 

The Size of a File

One of the routine operations the operating system performs consists of calculation the size of files it holds. This information is provided in terms of bits, kilobits, or kilobytes. To get the size of a file, the FileInfo class is quipped with the Length property. Here is an example of accessing it:

MsgBox("File Size: " & fleLoan.Length.ToString());

 

 

The Path to a File

Besides the name of the file, it must be located somewhere. The location of a file is referred to as its path or directory. The FileInfo class represents this path as the DirectoryName property. Therefore, if a file has already been created, to get its path, you can access the value of the FileInfo.DirectoryName property.

Besides the FileInfo.Directoryname, to know the full path to a file, you can access its FileSystemInfo.FullName property.

 

Practical Learning Practical Learning: Finding the Path to a File

  1. To display the path to the file in the title bar when the user opens it, change the Click event of the Open menu item as follows:
     
    Private Sub mnuFileOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click
            If openFileDialog1.ShowDialog() = DialogResult.OK Then
                ' Create a FileInfo object based on the file
                Dim fleLoan As FileInfo = New FileInfo(openFileDialog1.FileName)
                ' Open the file
                Dim stmLoan As StreamReader = fleLoan.OpenText()
    
                Text = "Loan Preparation: " & fleLoan.Name
    
                ' Use exception handling just in case
                Try
                    txtEmployeeNumber.Text = stmLoan.ReadLine()
                    txtEmployeeName.Text = stmLoan.ReadLine()
                    txtFullName.Text = stmLoan.ReadLine()
                    txtEmployerName.Text = stmLoan.ReadLine()
                    txtHomePhone.Text = stmLoan.ReadLine()
                    txtWorkPhone.Text = stmLoan.ReadLine()
                    txtLoanAmount.Text = stmLoan.ReadLine()
                    txtInterestRate.Text = stmLoan.ReadLine()
                    txtPeriods.Text = stmLoan.ReadLine()
                    txtPayment.Text = stmLoan.ReadLine()
                Finally
                    stmLoan.Close()
                End Try
    
                MsgBox("File Location: " + fleLoan.DirectoryName)
            End If
    End Sub
  2. Execute the application and try opening a file. Here is an example:
     
  3. Close the form(s) and return to your programming environment
 

The Attributes of a File

Attributes are characteristics that apply to a file, defining what can be done or must be disallowed on it. The Attributes are primarily defined by, and in, the operating system, mostly when a file is created. When the user accessed or open a file, to get its attributes, you can access the value of its FileSystemInfo.get_Attributes() property. This property produces a FileAttributes object.

When you create or access a file, you can specify or change some of the attributes. To do this, you can a FileAttributes object and assign it to the FileSystemInfo.set_Attributes() property.

FileAttributes is an enumerator with the following members: Archive, Compressed, Device, Directory, Encrypted, Hidden, Normal, NotContentIndexed, Offline, ReadOnly, ReparsePoint, SparseFile, System, and Temporary.

 
 

Previous Copyright © 2005-2016, FunctionX