943,550 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 7367
  • VB.NET RSS
Feb 22nd, 2009
0

How to check if a file is empty

Expand Post »
Ok, I know about how to check for the end of a file, but what I need to know, is if you create a new file, thats empty, how do you check if it really is empty, and if its the same as EOF, then what am I doing wrong with this code here:
VB.NET Syntax (Toggle Plain Text)
  1.  
  2. If Dir(My.Application.Info.DirectoryPath & "\TurbineData.xml") <> "" Then
  3.  
  4. If (TextBox1.Text.Equals("") Or TextBox2.Text.Equals("") Or TextBox3.Text.Equals("") Or TextBox4.Text.Equals("")) Then
  5.  
  6. Dim title2 = "Invalid Values"
  7. MsgBox(" Form contains no values to save.", , title2)
  8.  
  9. Else
  10. Dim ExistingData As New MyDatum
  11.  
  12. Dim Serializer As New System.Xml.Serialization.XmlSerializer(GetType(MyDatum))
  13.  
  14. If (File.Exists(My.Application.Info.DirectoryPath & "\TurbineData.xml" & Not EOF(1))) Then
  15.  
  16. Dim OpenStream As System.IO.FileStream = System.IO.File.Open(My.Application.Info.DirectoryPath & "\TurbineData.xml", IO.FileMode.Open)
  17.  
  18. ExistingData = Serializer.Deserialize(OpenStream)
  19.  
  20. OpenStream.Close()
  21. Else
  22. Dim err05 = "File does not Exist"
  23. MsgBox("Creating new XML file.", , err05)
  24.  
  25. End If
  26.  
  27. Dim NewData As New MyData
  28.  
  29. TextBox1.Focus()
  30. NewData.DateStamp = CDate(Date.Today()) 'DateValue(Now)
  31. NewData.TimeStamp = CDate(TimeString())
  32. NewData.Gap = gaptext.Text
  33.  
  34. ExistingData.Add(NewData)
  35.  
  36. Dim SaveStream As System.IO.FileStream = System.IO.File.Open(My.Application.Info.DirectoryPath & "\TurbineData.xml", IO.FileMode.Create)
  37.  
  38. Serializer.Serialize(SaveStream, ExistingData)
  39.  
  40. SaveStream.Close()
  41.  
  42. Dim title3 = "Save Alert"
  43. MsgBox("Data Saved to " & My.Application.Info.DirectoryPath & " \TurbineData.xml", , title3)
  44.  
  45. End If
  46.  
  47. Else
  48. Dim err05 = "File does not Exist"
  49. MsgBox("Creating new XML file.", , err05)
  50.  
  51. Dim SaveStream As System.IO.FileStream = System.IO.File.Open(My.Application.Info.DirectoryPath & "\TurbineData.xml", IO.FileMode.Create)
  52. SaveStream.Close()
  53. End If

Thats my code there, and the idea of this is that if the file is empty, then create a new entry in the xml, if the file already has data in it, then add to the file, dont overwrite the data.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Tamir09 is offline Offline
27 posts
since Feb 2009
Feb 22nd, 2009
0

Re: How to check if a file is empty

Ah.... You are posting VB.NET code in the VB 4/5/6 Forum.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Feb 23rd, 2009
0

Re: How to check if a file is empty

Oh I didnt know there was a difference...thought it was the same forum. I wonder if anyone has a solution to this?
Reputation Points: 10
Solved Threads: 0
Light Poster
Tamir09 is offline Offline
27 posts
since Feb 2009
Feb 23rd, 2009
0

Re: How to check if a file is empty

VB.NET Syntax (Toggle Plain Text)
  1. Private Sub RenameFile(ByRef fileNameFull As String)
  2. Dim fileI As FileInfo = New FileInfo(fileNameFull)
  3. If fileI.Exists Then
  4. Dim binReader As New BinaryReader(File.Open(fileNameFull, FileMode.Open))
  5. Dim fileClosed As Boolean = False
  6. Try
  7. ' read the first 4 bytes into a buffer to
  8. ' determine if the file is empty.
  9. Dim testArray As Byte() = {0, 0, 0, 0}
  10. Dim count As Integer = binReader.Read(testArray, 0, 3)
  11. If count <> 0 Then 'file NOT empty
  12. ' Reset the position in the stream to zero.
  13. binReader.BaseStream.Seek(0, SeekOrigin.Begin)
  14. 'do append stuff
  15. Else
  16. 'file empty
  17. End If
  18. Catch ex As Exception
  19. If Not fileClosed Then
  20. binReader.Close()
  21. fileClosed = True
  22. End If
  23. MsgBox("Uncaught general Exception error. " + ex.GetType().Name + vbNewLine + ex.Message)
  24. Finally
  25. If Not fileClosed Then
  26. binReader.Close()
  27. fileClosed = True
  28. End If
  29. End Try
  30. End If
  31. End Sub
Reputation Points: 18
Solved Threads: 2
Light Poster
edgar5 is offline Offline
30 posts
since Dec 2008
Feb 24th, 2009
0

Re: How to check if a file is empty

VB.NET Syntax (Toggle Plain Text)
  1. Dim reader() As String
  2. reader = File.ReadAllLines(spath)
  3. length = reader.GetLength(0)

If length=0 then your file is empty. I didn't checked this method with .xml, but with .txt it is working.
Reputation Points: 10
Solved Threads: 8
Junior Poster in Training
martonx is offline Offline
51 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Trying to create CSV file using VB.NET
Next Thread in VB.NET Forum Timeline: Save & Update





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC