ASP.NET Session 7


Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

ASP.NET Session 7

  1. 1. File Program Session 7
  2. 2. Objectives  File Namespace.  Working with file classes.  StreamWriters and StreamReaders.  Read & Write file handling program.
  3. 3. 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.
  4. 4. 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.
  5. 5. 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
  6. 6. 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
  7. 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.
  8. 8. 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:WorkLogFile.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.
  9. 9.  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:DevelopmentFileDemo directory and uses the relative path LogFile.txt, the file references would be C:DevelopmentFileDemoLogFile.txt.
  10. 10. 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);
  11. 11. • 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.
  12. 12. 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.
  13. 13. 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.
  14. 14. • 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");
  15. 15. Summary • File program concept. • Several property of System.IO Namespace. • Stream concept • StreamWriter & StreamReader.