Solved

StreamWriter in C#

Posted on 2006-11-22
4
6,019 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In this post we will learn different types of Android Layout and some basics of an Android App.
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

733 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