Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 22nd, 2007, 01:22 PM
Registered User
 
Join Date: Jun 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default FileStream vs StreamWriter

I am a new programmer and have been reading the Beginning C# book.

In chapter 22 (File System Data) they explain that you must use FileStream if you want to determine the location of data input to a file, and StreamWriter if you dont have a specified location.

Well I did code that set the area of beginning input using StreamWriter, and it works perfeclty fine. I made a very simple windows form application with a button as an event handler. The code is:

private void button1_Click(object sender, EventArgs e)
        {
            DateTime time = DateTime.Now;
            string logstring = textBox1.Text;
            FileStream loggy = new FileStream(@"C:\Log.txt", FileMode.Append, FileAccess.Write);
            loggy.Seek(0, SeekOrigin.End);
            StreamWriter log = new StreamWriter(loggy);
            log.WriteLine("");
            log.WriteLine(time);
            log.WriteLine(logstring);
            log.Close();
        }

Everytime you hit the button it does this:
(empty line)
Current time
Textbox value

Instead of using FileStream directly to write stuff in, all you have to do is set the Seek and then give the StreamWriter the FileStream file, which is automatically set to "Append" the file.







Similar Threads
Thread Thread Starter Forum Replies Last Post
StreamWriter problem cheh Visual Basic 2008 Essentials 1 August 26th, 2008 03:18 AM
How To Use StreamWriter Slago VS.NET 2002/2003 7 April 4th, 2006 03:32 PM
What object is streamwriter located under? kenn_rosie ASP.NET 1.0 and 1.1 Basics 0 February 21st, 2006 08:10 PM
How to convert HttpPostedFile to FileStream vinod_pawar1 HTML Code Clinic 1 August 13th, 2005 04:21 AM
SOS : FileStream Fileaccess Problem! aravinthan VS.NET 2002/2003 0 August 1st, 2003 05:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.