Home / Articles / Programming Languages / C# / C# File Input Output I

“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
Yashavant Kanetkar

Index

  1. File Class and FileInfo Class
  2. Reading And Writing To A Text File
Perhaps the only thing that works faster than the computer is computing jargon. It moves very, very fast. Often yesterday's rage is no longer today’s in–thing. Some jargon however has stuck around for long. GIGO (garbage in and garbage out) for instance. It has been a ground since the early days of computing. It has even matured over the years. Today GIGO happens automatically through a process called serialization…. A sure shot sign of man's progress!

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.

Comments

Log in or create a user account to comment.

On Sale From April 2008

Let Us C
8th Ed.
C programming classic & best seller. 1 million+ copies sold!

Y. Kanetkar

On Sale From April 2008

Introduction to Object Oriented Programming & C++

Y. Kanetkar

On Sale From Fall 2008

Microsoft .NET Framework: Web Application Security

Vijay Mukhi

On Sale From Nolvember 2008

Quest C++ Courseware
12+ hours of instructional audio and animated slides.

Y. Kanetkar Asang Dani

On Sale From November 2008

A Programmer's Guide to Web Application Security

Vijay Mukhi

Latest Forum Posts