C#
  Home arrow C# arrow Page 2 - C# StreamReader and StreamWriter Explained
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

C# StreamReader and StreamWriter Explained
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 12
    2007-02-06

    Table of Contents:
  • C# StreamReader and StreamWriter Explained
  • Using StreamWriter to write to a file
  • Step-by-step through the code example
  • Encoding with the classes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    C# StreamReader and StreamWriter Explained - Using StreamWriter to write to a file


    (Page 2 of 4 )

    You can have a StreamWriter object as the return value from calling the method File.CreateText(). You can also create a StreamWriter object using one of its constructor overloads. The following example illustrates using a StreamWriter object with a FileStream object to write characters to the file aFile.txt

    using System;
    using System.IO;

    namespace MyStreams
    {
      class Class1
      {
        public static void Main()
        {
          try
          {
            FileStream fs = new FileStream
    ("aFile.txt",FileMode.Create,
            FileAccess.ReadWrite,FileShare.None);
            string[] strings = {"C#", "ASP.NET", "XML"};
            using(StreamWriter sw = new StreamWriter(fs))
            {
              Console.WriteLine("This StreamWriter instance uses {0}
    to write to the file", sw.BaseStream);
              Console.WriteLine("The Property sw.Encoding returns:
    {0}",sw.Encoding);
              sw.WriteLine("www.aspfree.com");
              sw.WriteLine("contains many useful articles");
              sw.WriteLine("on many different technologies like {0},
    {1} and {2}", strings);
            }
            Console.WriteLine("Data has been written to the file");
            Console.ReadLine();
          }
          catch(IOException ex)
          {
            Console.WriteLine(ex.Message);
          }
        }
      }
    }

    Copy the above code and paste it in place of the auto-generated code of Class1.cs in your project, then run it and you will get the following screen shot:

    Navigate to the folder of the application to open the file aFile.txt.

    As you can see, there are three text lines written to this file. Let's walk through the code step-by-step

    More C# Articles
    More By Michael Youssef


       · In this simple article you are going to learn how to write to and read from a file...
     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT