Solved

StreamWriter in C#

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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 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…

707 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

18 Experts available now in Live!

Get 1:1 Help Now