asp.net session 7
DESCRIPTION
TRANSCRIPT
File Program
Session 7
Objectives
File Namespace. Working with file classes. StreamWriters and StreamReaders. Read & Write file handling program.
Introduction
• Reading and writing files are essential aspects of many .NET applications.
• Files can be a great way to store data using the .net application, or they can be used to transfer data between applications.
STREAMS
• All input and output in the .NET Framework involve the use of streams.
• A stream is an abstract representation of a serial device.
• A serial device is something that stores data in a linear manner and is accessed the same way: one byte at a time.
• This device can be a disk file, a network channel, a memory location, or any other object that supports reading and writing to it in a linear manner.
There are two types of streams:Output: Output streams are used when data is written to
some external destination. External destination: a physical disk file, a network
location, a printer, or another program.
Input: Input streams are used to read data into memory or
variables that your program can access. An input stream can come from almost any source
THE CLASSES FOR INPUT &OUTPUT
• The System.IO namespace contains almost all of the classes for read & write operation.
• few classes are contained in System.IO.
CLASS
File
Directory
Path
StreamReader
StreamWriter
The File and Directory Classes
• The File and Directory utility classes expose many static methods for manipulating.
• File classes have some method
METHOD DESCRIPTION
Copy() Copies a file from a source location to a target location.
Create() Creates a file in the specified path.
Delete() Deletes a file.
Open() Returns a FileStream object at the specified path.
Move() Moves a specified file to a new location. You can specify a different name for the file in the new location.
Directory classes have some method
Path Names and Relative Paths: When specifying a path name in .NET code, you can
use either absolute or relative path names.• An absolute path name explicitly specifies a file or
directory from a known location — such as the C: drive.
• C:\Work\LogFile.txt — this path defines exactly where the file is, with no ambiguity.
Method Description
CreateDirectory() Creates a directory with the specified path.
Delete() Deletes the specified directory and all the files within it.
Relative path names are relative to a starting location. By using relative path names, no drive or known
location needs to be specified.
• The current working directory was the starting point, which is the default behavior for relative path names.
• For example, if your application is running in the C:\Development\FileDemo directory and uses the relative path LogFile.txt, the file references would be C:\Development\FileDemo\LogFile.txt.
The StreamWriter Object
• The StreamWriter class enables you to write characters and strings to a file.
• The StreamWriter class handles the writing to the FileStream object.
FileStream aFile = new FileStream("Log.txt", FileMode.CreateNew);
StreamWriter sw = new StreamWriter(aFile);
• A StreamWriter object can also be created directly from a file:StreamWriter sw = new StreamWriter("Log.txt", true);
• If this is set to false, then a new file is created or the existing file is truncated and then opened.
• If it is set to true, then the file is opened and the data is retained. If there is no file, then a new one is created.
The StreamReader Object
Input streams are used to read data from an external source.
StreamReader objects are created in much the same way as StreamWriter objects.
The StreamReader Object
Input streams are used to read data from an external source.
StreamReader objects are created in much the same way as StreamWriter objects.
• StreamReader objects are created in much the same way as StreamWriter objects.
• FileStream aFile = new FileStream("Log.txt", FileMode.Open);
• StreamReader sr = new StreamReader(aFile);
• The StreamReader class can be created directly from a string containing the path to a particular file:
• StreamReader sr = new StreamReader("Log.txt");
Summary
• File program concept.
• Several property of System.IO Namespace.
• Stream concept
• StreamWriter & StreamReader.