StringWriter to Text file

R

Robert Linder

I am trying to write
'StringWriter SW = new StringWriter();' to a FileStream.
The code writes the text to file, but NotePad does not see the NewLines.

Below is the code snippet taken from a large project

////////////// Main ////////////////////////////////////////
StringWriter DataWriter ;

this.FileOut = new FileStream( @"c:\RegChk.txt" ,FileMode.Create );
this.BinaryOut = new BinaryWriter( this.FileOut );
-------------------------------------------------------------------------
StringWriter SW = new StringWriter();
this.SetWriter(SW);
-------------------------------------------------------------------------
SW.Close();

StringBuilder bu = SW.GetStringBuilder();
string dataSW = bu.ToString() ;

// print only of there is data
if ( dataSW.Length.Equals(0).Equals(false) )
{
string rKeyString = "Registry Key '"+rkeyName+"'\n";
this.BinaryOut.Write( rKeyString);
this.BinaryOut.Write(dataSW);
this.BinaryOut.Write("\n");
this.BinaryOut.Flush();
}
====================================================================================

public void SetWriter( StringWriter dataOut )
{
try
{
if ( ! dataOut.Equals(null) )
{
this.DataWriter = dataOut;
}
}
catch( System.NullReferenceException )
{
// error
}
}


private void PrintData( string msg , string key , string data )
{
this.DataWriter.Write( msg );
this.DataWriter.Write( " '");
this.DataWriter.Write( key );
this.DataWriter.Write( "' '");
this.DataWriter.Write( data );
this.DataWriter.Write( "\n");
}
 
N

Nicholas Paldino [.NET/C# MVP]

Robert,

Don't write "\n". Try using Environment.NewLine, and that should work.

Hope this helps.
 
R

Robert Linder

The Solution:
I added 'this.NL = Environment.NewLine;' to my code and replaced
Write("\n") with Write(this.NL)

Output is looks good in Notepad and gvim.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top