StreamWriter ?

J

JerryWEC

Hi all!

I'm creating a file using the following lines of code inside a property
set...

If Not File.Exists(value) Then
StreamWriter = File.CreateText(value)

StreamWriter.Close()

End If

When I look at this file it has a blank line (maybe a newline character).
I've been running like this for several months no problems. It just bugged
me that I had this extra line at the end of my log files.

Writing lines using...

StreamWriter.WriteLine("Some Text")

Now I'm trying to create an automatic clean up method to keep only the last
max lines. (In this case i'm using 1000 lines.) However, when I get to 1001
lines written. I use...

Dim oldLines() as String = File.ReadAllLines(MyFileName)

And it's getting the extra blank line. Is this normal to have an extra line
at the end of your files using the above code???

TIA! Jerry
 
J

Jesse Houwing

Hello JerryWEC,
Hi all!

I'm creating a file using the following lines of code inside a
property set...

If Not File.Exists(value) Then
StreamWriter = File.CreateText(value)
StreamWriter.Close()

End If

When I look at this file it has a blank line (maybe a newline
character). I've been running like this for several months no
problems. It just bugged me that I had this extra line at the end of
my log files.

Writing lines using...

StreamWriter.WriteLine("Some Text")

Now I'm trying to create an automatic clean up method to keep only the
last max lines. (In this case i'm using 1000 lines.) However, when I
get to 1001 lines written. I use...

Dim oldLines() as String = File.ReadAllLines(MyFileName)

And it's getting the extra blank line. Is this normal to have an
extra line at the end of your files using the above code???

TIA! Jerry

Are you using WriteLine or Write to write the last line? Writeline always
appends \r\n at the end (or another linefeed character based on the OS settings).
If you use Write for the last line you shouldn't get the empty line.

Easiest is probably checking if the line is empty and then just ignore it.

Jesse
 
W

Walter Wang [MSFT]

Thanks Jesse for your input.

Hi Jerry,

As Jesse said, using WriteLine() will always put a new line after the text.
You can either to make sure your last log is written to the file using
Write() or you can always use Write() and manually control when to write a
new line to it.

Please let us know if this helps or not.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Cowboy \(Gregory A. Beamer\)

Write just writes, WriteLine() adds a \r\n, as Jerry mentioned. The final
line will be null, so you can iterate through the file like so.

string line;

while (null != (line = reader.ReadLine())
{
}

As far as, is it normal? As mentioned, the \r\n is normal. You will also
find that most ASCII files you get, from a variety of programs, use a null
line as an endpointer. Some even use char(0), but that is another story.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
Co-author: Microsoft Expression Web Bible (upcoming)

************************************************
Think outside the box!
************************************************
 
W

Walter Wang [MSFT]

Hi Jerry,

I'm writing to check the status of this post. Please let us know if you
need anything else.

By the way, I just noticed you've also posted several questions in
newsgroup dotnet.framework.setup recently. I'm not sure if you've already
noticed that it's not a managed newsgroup. If you want replies from MSDN
Managed Newsgroup support team, you need to choose a newsgroup from here:
http://msdn2.microsoft.com/en-us/subscriptions/aa974230.aspx

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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