asp.net session 7
DESCRIPTION
TRANSCRIPT
![Page 1: ASP.NET Session 7](https://reader034.vdocuments.us/reader034/viewer/2022051412/549cb185b47959aa318b47d8/html5/thumbnails/1.jpg)
File Program
Session 7
![Page 2: ASP.NET Session 7](https://reader034.vdocuments.us/reader034/viewer/2022051412/549cb185b47959aa318b47d8/html5/thumbnails/2.jpg)
Objectives
File Namespace. Working with file classes. StreamWriters and StreamReaders. Read & Write file handling program.
![Page 3: ASP.NET Session 7](https://reader034.vdocuments.us/reader034/viewer/2022051412/549cb185b47959aa318b47d8/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051412/549cb185b47959aa318b47d8/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051412/549cb185b47959aa318b47d8/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051412/549cb185b47959aa318b47d8/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051412/549cb185b47959aa318b47d8/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051412/549cb185b47959aa318b47d8/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051412/549cb185b47959aa318b47d8/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051412/549cb185b47959aa318b47d8/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051412/549cb185b47959aa318b47d8/html5/thumbnails/11.jpg)
• 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](https://reader034.vdocuments.us/reader034/viewer/2022051412/549cb185b47959aa318b47d8/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051412/549cb185b47959aa318b47d8/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.us/reader034/viewer/2022051412/549cb185b47959aa318b47d8/html5/thumbnails/14.jpg)
• 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](https://reader034.vdocuments.us/reader034/viewer/2022051412/549cb185b47959aa318b47d8/html5/thumbnails/15.jpg)
Summary
• File program concept.
• Several property of System.IO Namespace.
• Stream concept
• StreamWriter & StreamReader.