Last Updated:

Processing File in .net

The wireframe contains two classes, File and File Info, which are very useful for working with files. The File class provides basic functionality for processing files in addition to read and write operations. Because the File class contains only static members, a file name is required as a parameter. The Filelnfo class has a constructor that creates an object that represents the file. Methods are then used to process that particular file.

Methods of the File class always perform a security check. If you want to continuously access a specific file, you can use the Filelnfo class, because in this class protection checking is performed only once, in the constructor. Protection is discussed in more detail in Chapter 13, "Protection".

File class

The File class contains methods for creating and opening files that return FileStreamStreamWriter, or StreamReader objects that actually read and write. The overloaded Create method returns a FileStream object.

The CreateText method returns a StreamWriter. The overloaded Open method, depending on the parameters passed, may create a new file or open an existing one for reading or writing. The object to return is a FileStream object. The OpenText method returns a StreamReader. The OpenRead method returns a FileStream object. The OpenWrite method returns an object of type FileStream.

The File class also contains methods for copying, deleting, and moving files. In addition, we can check the existence of the file. The following file attributes can be read and modified:

  • Creation time
  • the time of the last request;
  • Recent recording time;
  • archival, hidden, normal, system or temporary;
  • compressed, encrypted;
  • Read-only
  • is the file a directory?

Path class

 

Very often, the file name passed as an input parameter must be the full path to the file. But it would often be more convenient to process only parts of the path. This can be done quite simply by using the static methods of the Path class. The Path class contains static fields that point to various platform-specific portions of the path name, such as delimiter characters for directories, paths, and volumes, as well as prohibited characters in the file name and access paths.

The static methods of this class allow you to change the file extension or find the folder that contains temporary files. The GetFullPath method is particularly useful. You can pass it a relative path, such as \foo.txt, and it will return the full path of the file. This is very useful for the File class or protection classes for which you want to specify the full path to the file.

Filelnfo class

The Filelnfo constructor creates an object that represents a disk file. The constructor takes one parameter, a string that contains the file name. The object will have properties that reflect the properties of the file, such as the creation time, size, and full path to the file. The class contains methods for creating and opening a file that are similar to the methods of the File class, but work with a specific instance of the file and therefore do not need a parameter such as a file name. The Filelnf o class also contains methods that enable you to move and copy files.

Sample File

 

The File in The FilelO folder example illustrates the use of the Filelnfo and  File classes.

This example uses the static Delete method of the File class to delete the specified file. The static CreateText method then creates a new file and returns a StreamWriter instance that is used to write text to the file.

Next, the thread is closed, and the static Move method renames the file. Then, an instance of Filelnfo is created that will represent this renamed file.

The full file name, size, and date it was created are printed to the console. The file is opened as a text file, and then a streamReader instance is used to read and display the contents of the file to the console.

 

File::Delete("file2.txt"); // Delete file "file2.txt"
StreamWriter *sw =
System::IO::File::CreateText("file.txt");
sw › Write("The time has come the Walrus said, "); // Talk...
sw › WriteLine("to talk about many things.");
sw › Write("Of shoes, and ships, and sealing wax, "); // about boots, and courts, and sealing wax
sw › WriteLine("of cabbages and kings."); // cabbage and kings
sw › Write("And why the sea is boiling hot, "); // And why
// the sea is boiling
sw › WriteLine("and whether pigs have wings."); // and whether
// pig wings.
sw Close();
File::Move("file.txt", "file2.txt");
//File:: Forward("file.txt", "file2.txt");
FileInfo *fileInfo = new FileInfo("file2.txt");
Console::WriteLine(
"File {0} is {1} bytes in length, created on {2}",
// "File {0} - {1} bytes, created by {2} ",
fileInfo › FullName,
_box(file!nfo › Length), // Length
_box(file!nfo › CreationTime));
Console::WriteLine("");
StreamReader *sr = file!nfo › OpenText();
String *s = sr › ReadLine();
// Line
while (s!=0)
// while (s!= 0)
{
Console::WriteLine(s);
s = sr › ReadLine();
}
sr Close();
Console::WriteLine("");