Solved

StreamWriter in C#

Posted on 2006-11-22
4
6,021 Views
Last Modified: 2012-06-27
I want to append to the end of a file.  I have done this:

                        string filename = outpath + @"\" + fname + ".txt";
                        if(File.Exists(filename))
                        {
                              fs = File.OpenWrite(filename);
                        }
                        else
                        {
                               fs = File.Create(filename);
                        }

                        StreamWriter sw;
                        sw =       new StreamWriter(fs);
                        
                              sw.WriteLine(ohlc);

This writes to the begining of the file.  Please advice how I can set the pointer to the end oif the file.  Thx
0
Comment
Question by:khyberman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 2

Expert Comment

by:amiableansari
ID: 18000837

Hi

ho this if file exists then call the AppendText method

.....
if(File.Exists(filename))
{
      fs = File.AppendText(filename);                        
}
....

and the else condition remains same. you can however replace Create function with CreateText
0
 
LVL 22

Expert Comment

by:cookre
ID: 18000998
sw =      new StreamWriter(fs,true);  // this will append
0
 
LVL 2

Accepted Solution

by:
amiableansari earned 250 total points
ID: 18001003

you can directly create stream writer as

string filename = outpath + @"\" + fname + ".txt";
StreamWriter sw;
if(File.Exists(filename))
{
      sw = File.AppendText(filename);                        
}
else
{
      sw = File.CreateText(filename);
}
                          
sw.WriteLine(ohlc);
0
 

Author Comment

by:khyberman
ID: 18002526
Thans! Can I also create a Binary file in a similar fashion?
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Simple Linear Regression

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question