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.

Alternatively, if you want, you can provide any valid
directory you have access to. In this case, you should provide the complete
path.

File Creation

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:

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 time, this method directly returns a StreamWriter
object. You can use this returned object to write text to the file.

File Existence

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 the right 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 doesn't exist or it doesn't exist in the path.

As mentioned earlier, the FileInfo.Create() method
returns a 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. This method returns a StreamWriter object. The StreamWriter
class is based on the TextWriter class that is equipped with Write()
and WriteLine() methods used to write values to a file. The Write()
method writes text on a line and keeps the caret on the same line. The WriteLine()
method writes a line of text and moves the caret to the next line.

After writing to a file, you should close the StreamWriter
object to free the resources it was using during its operation(s).

Display the Employees form and create one employee with a full name
and an employee number (remember the employee number)

Close the forms and return to your programming environment

Appending to a 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.