Community-Credit.com | NonProfitWays.com | SOAPitstop.com   Skin:   
      User: Not logged in 
Home
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Presentations
Featured Articles
Book Chapters
Training Courses
Events
NewsGroups
 
Discussions
Examples
Tutorials
Tools
Articles
Resources
Websites
 
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
Logout
 
About Me
My Blog
HeadGeek Articles
Talking Portfolio
Resume
Pictures
World Trip Pics

File IO in Asp.Net
Author: Rajendra Kumar
Published: 6/26/2006 5:15 AM
Category: ASP.NET
Summary: This Article describes about File IO in Asp.Net

 

File I/O

 

Some Applications require Reading, Writing, Manipulating of Directories and Files. For such operation there is a namespace System.IO in .net framework, provides a group of methods and classes. In the .NET Framework, the System.IO namespace is the region of the base class libraries devoted to file-based input and output services

 The following are the key classes that are present in The System.IO namespace.

 

  1. BinaryReader
  2. BinaryWriter
  3. Directory
  4. DirectoryInfo
  5. File
  6. FileInfo
  7. BufferedStream
  8. FileStream
  9. MemoryStream
  10. StreamReader
  11. StreamWriter
  12. StringReader
  13. StringWriter

 

Let us discuss about all the classes in briefly

 

BinaryReader: Allows you to read primitive datatypes as binary values.

BinaryWriter:  Allows you to write primitive data types  as  binary values.

Directory:        It Contains static methods to create, move, and enumerate directories and subdirectories. The static methods of the Directory class do a security check on all methods

DirectoryInfo: it Contains instance methods to create, move, and enumerate directories and subdirectories. If you want to reuse an object multiple times, use DirectoryInfo instead of Directory to avoid security checks

Eg:

 

        DirectoryInfo  dir = new DirectoryInfo("D:\\Example");

               dir.CreateSubdirectory("SubDir1");

 

 

File                :It Contains static methods to create, copy, delete, move, and open files, and assists in the creation of FileStream objects

 

    FileInfo:     it Contains instance methods to create, copy, delete, move, and open files, and assists in the creation of FileStream objects. Use FileInfo if you want to reuse the File instance on a single file.

 

FileInfo file1 = new FileInfo("D:\\SampleFile1.txt");

                  StreamWriter text1 = file1.CreateText();

                  string str1 = "Hello Welcome to FileIO";

                  text1.WriteLine(str1);

                  text1.Write(text1.NewLine);

                  text1.Close();

                  Response.Write("File Created");

The above is the example of writing a Text File

 

BufferedStream:it Offers a temporary location to read or write info. later it can be moved to permanent storage.

 

FileStream: this Provides implementations for the abstract stream class suitable for file-based streaming (random file access). The FileStream class can open a file in one of the two modes, either synchronously (Read and Write methods) or asynchronously(BeginRead and BeginWrite methods).

   

MemoryStream  Creates Streams that have memory as  a backing store instead of a disk  or a network connection

StreamReader:It implements a TextReader that reads characters from a byte stream to particular encoding.

StreamWriter :It implements a TextWriter object for writing characters to a stream in a particular encoding

StringReader :It Implents a TextReader object that implements that reads from a string.

StringWriter: Implements a TextWriter object that writes information to a string. The information is stored in an underlying StringBuilder Class .

Let us see some examples

Example1:Example to create a Directory and SubDirectory

DirectoryInfo  dir = new DirectoryInfo("D:\\Example");

               dir.CreateSubdirectory("SubDir1");

 

 

Example2:Example to create File

FileInfo file1 = new FileInfo("D:\\SampleFile1.txt");

                  StreamWriter text1 = file1.CreateText();

 

 

 

Example3: FileInfo file1 = new FileInfo("D:\\SampleFile1.txt");

                  StreamWriter text1 = file1.CreateText();

                  string str1 = "Hello Welcome to FileIO";

                  text1.WriteLine(str1);

                  text1.Write(text1.NewLine);

                  text1.Close();

                  Response.Write("File Created");

 

Example4:Reading a Text File

StreamReader read1 = File.OpenText("C:\\SampleFile1.txt"); 

                  String str2 = null;

 

                  while((str2 = read1.ReadLine())!= null)

                  {

                        Response.Write(str2);

                  }

            read1.Close();

Example5:StringWriters

StringWriter write = new StringWriter();

                    write.WriteLine("Hello welcome ");

                    write.WriteLine("How are You?");

              write.Write(write.NewLine);

                    write.Close();

Response.Write(write.ToString());

 

Summary

 

This Article Explains about Fil IO in Asp.net

 


 

 
Fans of "The Office"
Dwight Bobbleheads are here!
  “It's me! I'm the bobblehead! Yes!”



Advertise on XMLPitstop

Advertise on XMLPitstop


EggHead Cafe
Web Servicee development
DotNetSlackers
Buy text ads here!
remote file backup
Color Laser Printer
Fendi sunglasses
Skype vs. sipcall
VoIP Internettelefonie
Buy text ads here!

Interested in Text ads?
2,223 Total Members
36 members(last 30 days)
8 members(last 7 days)
2 members(today)

1,609 Total Discussions
12 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

17,260 Total Blog Posts
1,839 Blogs(last 30 days)
342 Blogs(last 7 days)
49 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

13,786 Total Resources
5 Resources(last 30 days)
1 Resources(last 7 days)
0 Resources(today)


 

David Silverlight's XMLPitstop.com| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139