“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.
C# Programming Series
C# File Input Output I
Yashavant Kanetkar
File Class and FileInfo Class
.NET offers two classes for file operations—the File class and the FileInfo class. Both of these classes are defined in the System.IO namespace hence we need to write the statement using System.IO in the program that uses File or FileInfo class.
The File class is derived from the Object class. It contains only static methods. We are not allowed to create an object of the File class. The FileInfo class is derived from FileSystemInfo class. The FileSystemInfo class is derived from the MarshalByRefObject class which in turn is derived from the Object class. We can create an object of the FileInfo class. The hierarchy of these classes is shown in Figure 1.
Let us now understand the objective of creating two classes for file operations. The static functions of the File class can be called to perform various file operations without creating an object. This avoids the overhead of creating objects. So, if we wish to carry out single operation on the file, we can use the File class.
As against this, to call the member functions of the FileInfo class it is necessary to create an object. This is because FileInfo class contains non-static member functions. So, if we wish to carry out multiple operations on the file (with the preservation of state of the object) we can use the FileInfo class. When we create a FileInfo object, all the relevant information of the file like size, attributes, authentication permissions are stored in the data members. This information can then be used by other functions while carrying out multiple operations. If we use the File class for carrying out multiple operations then this information will have to be read each time we perform a new operation.
Also, at times we are required to pass the file information to another application. In such a case it is necessary to create a FileInfo object and then pass its state to other application (this process is known as marshalling). Marshalling of object is possible only if the class is derived from the MarshalByRefObject class. Since the object of the File class cannot be created, it has not been inherited from MarshalByRefObject class.



