StreamWriter problem

  • Thread starter Thread starter Baz
  • Start date Start date
B

Baz

Hi All,

Below is some sample code from a .NET sub I created. It seems to work
OK, but sometimes when I call the routine and pass it a string, the
actual data written to the file is followed by a lot of blank space. I
haven't counted the amount of empty data logged to the file, but it is
thousands and thousands of bytes. The strange thing is that it only
happens sometimes, and not others. Am I doing something completely
wrong? Can anyone see a flaw in the code?

Apologies if this code is not the best way of implementing the desired
solution - I am a very new convert to .NET

Thanks,

Baz.

=====
Public Sub LogIt(ByVal Message As String)

Dim sFilename As String = Application.StartupPath & "\Test.txt"
Dim sr As New StreamWriter(sFilename, True)
sr.Write("[" & Now.Date.ToString & "] [" & Now.TimeOfDay.ToString &
_
"] " & Message)
sr.Close()

End Sub
=====
 
i doubt the problem is in this code, it is possible you are sending the
spaces in your message in that case
"] " & Message.Trim())
would help

in any case it looks strange.

hope you find the problem

eric

Baz said:
Hi All,

Below is some sample code from a .NET sub I created. It seems to work
OK, but sometimes when I call the routine and pass it a string, the
actual data written to the file is followed by a lot of blank space. I
haven't counted the amount of empty data logged to the file, but it is
thousands and thousands of bytes. The strange thing is that it only
happens sometimes, and not others. Am I doing something completely
wrong? Can anyone see a flaw in the code?

Apologies if this code is not the best way of implementing the desired
solution - I am a very new convert to .NET

Thanks,

Baz.

=====
Public Sub LogIt(ByVal Message As String)

Dim sFilename As String = Application.StartupPath & "\Test.txt"
Dim sr As New StreamWriter(sFilename, True)
sr.Write("[" & Now.Date.ToString & "] [" & Now.TimeOfDay.ToString &
_
"] " & Message)
sr.Close()

End Sub
=====
 
Hi Baz,

This are anoying problems, and from here it is impossible to see, you can do
two things,
You can do some checking before just to test for that I asume it is a
windowsforms application.
You can cut of the message for the lenght you want, for both I write 100
take what it should be here.
Both solutions are written in line in the code.

I hope this helps,

Cor

Public Sub LogIt(ByVal Message As String)

Dim sFilename As String = Application.StartupPath & "\Test.txt"
Dim sr As New StreamWriter(sFilename, True)
'Only to test and should be removed with the second solution
if message.length >100 then
messagebox.show(message)
end if
sr.Write("[" & Now.Date.ToString & "] [" & Now.TimeOfDay.ToString &
_

"] " & Message.substring(0,100)
 
Thanks to all for the replies. As always, the problem was not in the
code snippet I attached. What happened was I was logging data that was
returned from a socket DataArrival event. I was using a third party
socket class, and it seems that the socket buffer variable was a fixed
byte array of size ~32000. This was causing all my blank space in the
output file.

For some reason, the blank data did not appear in the Watch window, so I
didn't think this was the problem initially.

Thanks again for the help. One of these days I'll get my head around
..NET (maybe!)
 
Back
Top