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:

DateTime dteCreationTime = fleLoan.CreationTime;
MessageBox.Show("Date and Time Created: " + dteCreationTime.ToString());

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:

MessageBox.Show("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 void mnuFileOpen_Click(object sender, System.EventArgs e)
    {
    	if( openFileDialog1.ShowDialog() == DialogResult.OK )
    	{
    // Create a FileInfo object based on the file that contains the employees
    		FileInfo fleLoan = new FileInfo(openFileDialog1.FileName);
    		// Open the file
    		StreamReader stmLoan = fleLoan.OpenText();
    
    		Text = String.Concat("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();
    		}		
    	}
    }
  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:

MessageBox.Show("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:

MessageBox.Show("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 void mnuFileOpen_Click(object sender, System.EventArgs e)
    {
    	if( openFileDialog1.ShowDialog() == DialogResult.OK )
    	{
    // Create a FileInfo object based on the file that contains the employees
    		FileInfo fleLoan = new FileInfo(openFileDialog1.FileName);
    		// Open the file
    		StreamReader stmLoan = fleLoan.OpenText();
    
    		Text = String.Concat("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();
    		}
    
    		MessageBox.Show("File Location: " + fleLoan.DirectoryName);		
    	}
    }
  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-2012 FunctionX