Solved

StreamWriter in C#

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 …
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 …

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now