.NET Framework - streamwriter schreibt nicht vollständige_zeilen

Asked By Daniel Zuidinga
17-May-10 11:39 AM
Hi,

ich erstelle ganz normal mit    myWriter = new StreamWriter(path,false,
System.Text.Encoding.Default);
einen Streamwriter und schreibe recht lange Zeilen mittels WriteLine.
Die langen Zeilen werden nicht oder nur zum Teil in die Datei
geschrieben. Was mach ich falsch?

Gru?
StreamWriter
(1)
NETMaster
(1)
Encoding
(1)
Buffer
(1)
WriteLine
(1)
Flush
(1)
Daten werden
(1)
Sicherheit
(1)
  Thomas Scheidegger replied to Daniel Zuidinga
17-May-10 11:51 AM
Hallo Daniel


nur zur Sicherheit:
Streams haben oft (diverse) Buffer,
Daten werden nicht sofort in Datei geschrieben.
Evtl. hilft ein  Flush(), wenn wirklich n?tig.



--
Thomas Scheidegger - 'NETMaster'
http://dnetmaster.net/
  Frank Dzaebel replied to Daniel Zuidinga
17-May-10 02:00 PM
Hallo Daniel,


Sch?n w?re zun?chst ein reproduzierbares Beispiel.

[Short but complete programs]
http://www.yoda.arachsys.com/csharp/complete.html

Folgende M?glichkeiten fallen mir als erstes ein.
(sie sind gut in der Doku beschrieben, so lasse ich
mal einen eigene Kommentar dazu) :

[StreamWriter.Close-Methode (System.IO)]
http://msdn.microsoft.com/de-de/library/system.io.streamwriter.close.aspx

[StreamWriter.AutoFlush-Eigenschaft (System.IO)]
http://msdn.microsoft.com/de-de/library/system.io.streamwriter.autoflush.aspx

[StreamWriter.Flush-Methode (System.IO)]
http://msdn.microsoft.com/de-de/library/system.io.streamwriter.flush.aspx

Es kann in einigen F?llen nur ein Anzeige-Problem sein.
Je nach Ansichts-Editor kann das Ende der ~langen (was ist lang? ;-)
Zeile schwer erreichbar sein.
Wichtig w?re auch, ob Du ggf. einen Shared-ReadWrite benutzt o.?..
Alles Dinge, die Einfluss haben k?nnen.


ciao Frank
--
Dipl.Inf. Frank Dzaebel [MCP/MVP C#]
http://Dzaebel.NET
Create New Account
help
call Flush() or Close(), bytes do get written to a file? b) Does FileStream also buffer input bytes? If so, then what exactly does that mean? That bytes may not get FileStream to get written into variable? 2) a) StreamReader also buffers data? b) I assume StreamWriter also buffers data internally, waiting for a buffer to be full before it gets written to the underlying stream? 3) But if both StreamWriter and FileReader already buffer data internally, then why would we also need special classes for buffering? 4) I assume that if WriteStream ( or any other stream buffer ) object buffers data, then data will only get written to a file when we either call Flush(), Close() or when internal buffer is full? 5) a) Does OS( or its disk buffer) also buffer data from our streams? b) If so, then I assume that calling Flush
return as I wrote due to deadlock. void SessionExited(object sender, EventArgs e) { Manager.con.WriteLine("E Event SessionExited ENTRY " + HostName); Manager.con.WriteLine("E Event thread " + Thread.CurrentThread.ManagedThreadId); if (Lock(true)) { try { Manager.con.WriteLine( String.Format("E Session Exit on {0} {1}", HostName, (_session.Shell ! = null ? ("exit code " + _session RemoteManager.IsAlive()) _session.RemoteManager.ShutdownServer(); } catch { } finally { _session.RemoteManager = null; _session.Shell = null; Manager.con.WriteLine("E Event SessionExited DONE " + HostName); Lock(false); } } else { Manager.con.WriteLine("E Event Exit blocked " + HostName); } } And here's the manual Endsession() routine: public void EndSession() { Manager.con.WriteLine("S EndSession ENTRY " + HostName); Manager.con WriteLine("S EndSession thread " + Thread.CurrentThread.ManagedThreadId); while (!Lock(true)) { Thread.Sleep(100); } if (this.Alive && this.Active && InShellSession && InRemoteSession) { Console.WriteLine("S Ending Remote Session on host " + HostName); / / this btw. seems to have no effect - I
open up in RichTextBox2, the first bit works fine but the second bit 'I.O.StreamWriter' I cannot seem to find the correct code. I can get the "Text Written to txt" If System.IO.File.Exists(FILE_NAME) = True Then Dim objWriter As New System.IO.StreamWriter(FILE_NAME) RichTextBox2.Text = objWriter.Write objWriter.Close() MsgBox("Text written to file") Else MsgBox("File Does Not Exist") End If End Sub The Write method of the StreamWriter class does not return a value; in VB terms, it is implemented as a Sub build a string of data that was going to the file, write it through the StreamWriter instance, and separately copy it to the display control (the RichTextBox, in this case). - - Jack If System.IO.File.Exists(FILE_NAME) = 3D True Then Dim objWriter As New System.IO.StreamWriter(FILE_NAME) Using fs As New System.IO.FileStream _ ("C: \ sorted.txt", System.IO.FileMode txt" If System.IO.File.Exists(FILE_NAME) = True Then Dim objWriter As New System.IO.StreamWriter(FILE_NAME2) It may seem a little, but I do feel I have made some progress create a text file: Dim oFile as System.IO.File Dim oWrite as System.IO.StreamWriter oWrite = 3D oFile.CreateText( = 93C: \ sample.txt = 94) And I where would it go? It a string with the name of the file. The command you have for creating the StreamWriter is also good, you are just not sending it any data. Actually, on second thought
I have a log class that makes a synchronized TextWriter like so, in the constructor: StreamWriter sw = new StreamWriter(filename); tw = TextWriter.Synchronized(sw); In the destructor, ~MyLogClass(), I call: tw.WriteLine("some stuff"); tw.Close(); The Close() call throws an exception saying I can't do use it? Are resources' Dispose() auto-invoked or something? What's strange is that the WriteLine doesn't throw an exception (although its contents never make it to the file). Zytan deal with unmanaged resources. Bear in mind that by the time *your* finalizer runs, the StreamWriter's finalizer may already have run. It sounds like you may well not want a underlying streams have Finalize methods to close themselves. The result is that the writer's buffer is never flushed to the stream before the stream is closed, resulting in lost data (it's designed this way). I assume WriteLine executes error free because no attempt is made to flush to the closed stream. Zytan avoid Finalizers for unmanaged code because stuff like this happens. Try a syntax like: using (StreamWriter stream = new FileInfo(@"c: \ test123").CreateText()) { stream.WriteLine("blah blah blah"); } True, but streams do