asp.net session 7

15
File Program Session 7

Upload: sisir-ghosh

Post on 25-Dec-2014

217 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: ASP.NET Session 7

File Program

Session 7

Page 2: ASP.NET Session 7

Objectives

File Namespace. Working with file classes. StreamWriters and StreamReaders. Read & Write file handling program.

Page 3: ASP.NET Session 7

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.

Page 4: ASP.NET Session 7

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.

Page 5: ASP.NET Session 7

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

Page 6: ASP.NET Session 7

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

Page 7: ASP.NET Session 7

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.

Page 8: ASP.NET Session 7

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.

Page 9: ASP.NET Session 7

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.

Page 10: ASP.NET Session 7

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);

Page 11: ASP.NET Session 7

• 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.

Page 12: ASP.NET Session 7

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.

Page 13: ASP.NET Session 7

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.

Page 14: ASP.NET Session 7

• 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");

Page 15: ASP.NET Session 7

Summary

• File program concept.

• Several property of System.IO Namespace.

• Stream concept

• StreamWriter & StreamReader.