Delphi.net Basics
  Home  |  Delphi .net Home  |  Framework Files and Folders
 .NET Framework
 Namespace References

 System
 System.Collections
 System.Drawing - download only
 System.Globalization
 System.IO

 Articles and Tutorials

 Overview of .NET
 Delphi and .NET
 Winform Applications
 ASP .Net Applications
 ASP Web Services
 Framework Collections
 Framework String Handling
 Framework Files and Folders


 Author links

 

 
 
 Framework Files and Folders
File and Folder IO operations
The .Net Framework provides a rich set input/output mechanisms. The key classes are shown here (click on a blue item to see the reference information) :
 
 File
   - A set of static methods for preparing a file for processing
 
 FileInfo
   - A class for preparing a single file for multiple processing
 
 Directory
   - A set of static methods for preparing a directory for processing
 
 DirectoryInfo
   - A class for preparing a single directory for processing
 
 Path
   - A set of static methods for handling a file path definition
 
 StreamReader
   - Allows a file or other source to be read as a character stream
 
 StreamWriter
   - Allows a file or other source to be written as a character stream
 
 BinaryReader
   - Allows a file or other source to be read as a binary byte stream
 
 BinaryWriter
   - Allows a file or other source to be written as a binary byte stream
 
 MemoryStream
   - Allows a block of memory to be treated as a data source
 
 FileSystemWatcher
   - Allows a a directory or files to be monitored for change activity


Principles or file access
As with most programming languages, access to a file requires some kind of file handle. In the Console Application below, we use a FileInfo object - this acts as our file handle after we have associated it with a real file - 'C:\DelphiBasics.txt'.
 
In order to write to this file (handle), we ask the FileInfo object to generate a StreamWriter object. We then use a method of this object to write to the file. Note that the creation of the StreamWriter object (Writer) also opens the file for writing to.
 
When we want to read from the file, we ask the FileInfo object for a StreamReader object, which again automatically gets opened upon creation.
 
These particular stream handling objects work on characters (or lines of characters).
 
 program Project1;
 {$APPTYPE CONSOLE}
 
 uses
   System.IO;
 
 var
   FileInfo : System.IO.FileInfo;
   Reader   : System.IO.StreamReader;
   Writer   : System.IO.StreamWriter;
   MyByte   : Integer;
 
 begin
   // Create a FileInfo object for a text file
   FileInfo := System.IO.FileInfo.Create('C:DelphiBasics.txt');
 
   // Open the file for writing
   Writer := FileInfo.CreateText;
 
   // Write to the file
   Writer.Write('A');
   Writer.Write('B');
   Writer.Write('C');
 
   // Close the file
   Writer.Close;
 
   // Re-open for reading
   Reader := FileInfo.OpenText;
 
   // Display the contents
   Console.WriteLine(Reader.ReadToEnd);
 
   // Close the file
   Reader.Close;
 
   Console.Readline;
 end.

The program yields this output :
 
 ABC

If you refer to the System.IO class reference pages, you can see examples of binary file access, and all of the other classes mentioned above.
 

The Path class
Path objects contain platform specific information about the location of a directory or folder (on the current or a networked machine). The path string can be parsed to extract the different components, where appropriate for the current platform, such as the hard disk drive (such as C:\) and the directory, name and extension of the file.
 
These are useful static methods for looking, for example, at the output from a DirectoryInfo GetFiles method execution.
 

Creating, deleting and moving files and folders
The File, FileInfo, Directory and DirectoryInfo classes additionally provide methods for manipulating files and folders themselves, rather than their contents. In addition to file/folder creation, deletion, and move operations, you can also read and write their creation, last read and last write dates and times, where authorised. These all work around the DateTime structure - similar in operation to the native TDateTime class.
 

Some unusual classes
The MemoryStream class allows a block of memory to be treated as a block of bytes as if we were writing an assembly language program. This gives great flexibility to programmers when dealing with unusual data collections.
 
Necessarily, access to the block bytes can be direct, but also sequential, as if reading or writing to/from a file.
 
The FileSystemWatcher class is fairly unusual in having events - invoked when the files or folder being watched have been changed. You can watch for new files, file changes or deletions, and similarly with directories (folders). This is the kind of class needed when writing an application that presents directory contents that must be kept up to date.
 
 
'
'
'
Delphi Programming © Neil Moffatt All rights reserved.  |  Home Page