473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

StreamWriter couldn't append data in file

Hi ,
I had a strang problam ,when i use StreamWriter to append to a file,i
found that if i don't close the
StreamReader it couldn't write the data into file,the code as folllow
that:
class Program
{
static void Main(string[] args)
{
FileInfo file = new FileInfo(@"E:\Demo\Log\20061214.log");
StreamWriter debugWriter = new
StreamWriter(file.Open(FileMode.Append, FileAccess.Write,
FileShare.ReadWrite));

debugWriter.Write("dasdasdasdsadsadsadasdasd123354 856");
debugWriter.Close();
}
}

however,if remove the debugWriter.Close(),the problam show,why ?can
anyone help me?

Thanks

Dec 14 '06 #1
2 5409
Hi Kevien,

When you write using a StreamWriter, you are just writing to the buffer,
not to the underlying stream. This buffer is flushed to the underlying
stream using Flush or Close, or by setting AutoFlush = true.

When you call debugWriter.Close the buffer is flushed to the file.

A good method to ensure you flush the buffer is the 'using' statement
which will automatically call close/dispose on the object when it goes out
of scope.
static void Main(string[] args)
{
FileInfo file = new FileInfo(@"E:\Demo\Log\20061214.log");
using(StreamWriter debugWriter = new
StreamWriter(file.Open(FileMode.Append, FileAccess.Write,
FileShare.ReadWrite)))
{
debugWriter.Write("dasdasdasdsadsadsadasdasd123354 856");
}
}
On Thu, 14 Dec 2006 09:46:03 +0100, Kevien Lee <ir************@gmail.com
wrote:
Hi ,
I had a strang problam ,when i use StreamWriter to append to a file,i
found that if i don't close the
StreamReader it couldn't write the data into file,the code as folllow
that:
class Program
{
static void Main(string[] args)
{
FileInfo file = new FileInfo(@"E:\Demo\Log\20061214.log");
StreamWriter debugWriter = new
StreamWriter(file.Open(FileMode.Append, FileAccess.Write,
FileShare.ReadWrite));

debugWriter.Write("dasdasdasdsadsadsadasdasd123354 856");
debugWriter.Close();
}
}

however,if remove the debugWriter.Close(),the problam show,why ?can
anyone help me?

Thanks


--
Happy Coding!
Morten Wennevik [C# MVP]
Dec 14 '06 #2
I'm not sure I understand what you are saying... however, if you leave
a file open then it is not guaranteed to commit unflushed data. What
exactly are you seeing? And note that you should probably be "using"
the writer.

Marc
Dec 14 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jonathan Buckland | last post by:
Can someone give me an example how to append data without having to load the complete XML file. Is this possible? Jonathan
7
by: MLH | last post by:
A97 has menu options that support exporting table data to ms excel data file format. Is this easily implemented from within code? Any examples? I looked in A97 HELP for the TransferSpreadsheet...
14
by: vbMark | last post by:
Greetings, This seems like it should be simple but I can't figure out how to do this. I just want to append binary file 2 on to the end of binary file 1. Sample code please? Thanks!
1
by: Amjad | last post by:
How can I append a text file to another? Amajd
6
by: Don | last post by:
I'm having problems working with a streamwriter object. After closing the streamwriter and setting it to Nothing, I try to delete the file it was writing to, but I always get the following error...
12
by: Steven Bethard | last post by:
Ok, so I have a module that is basically a Python wrapper around a big lookup table stored in a text file. The module needs to provide a few functions:: get_stem(word, pos, default=None)...
4
by: jm0 | last post by:
Hi, im developing this program, and then i ran into some trouble "oooh no!" hate to ask.. have search google and this page for something to spilt things up in arrays or whatever can be used, spent...
2
by: defn noob | last post by:
from Tkinter import * import os master = Tk() w = Canvas(master, width=800, height=600) print os.path.exists('C:/me/saftarn/desktop/images/blob4.jpg') im = PhotoImage(file =...
5
by: koksamsun | last post by:
Hi all I've created a some sheets of .xls file like sample_sheet1.xls sample_sheet2.xls sample_sheet3.xls Now i want to append these three sheets.xls in sample.xls main file in same folder....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.