Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2007

Tip: Looking for answers? Try searching our database.

StreamWriter ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JerryWEC - 06 Aug 2007 19:59 GMT
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
Jesse Houwing - 06 Aug 2007 20:39 GMT
Hello JerryWEC,

> Hi all!
>
[quoted text clipped - 26 lines]
>
> 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
Walter Wang [MSFT] - 07 Aug 2007 03:31 GMT
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 (wawang@online.microsoft.com, 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.
Cowboy (Gregory A. Beamer) - 07 Aug 2007 14:14 GMT
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.

Signature

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!
************************************************

> Hi all!
>
[quoted text clipped - 26 lines]
>
> TIA! Jerry
Walter Wang [MSFT] - 13 Aug 2007 01:48 GMT
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 (wawang@online.microsoft.com, 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.