Solved

StreamWriter in C#

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

770 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