954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to check if a file is empty

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:

If Dir(My.Application.Info.DirectoryPath & "\TurbineData.xml") <> "" Then

            If (TextBox1.Text.Equals("") Or TextBox2.Text.Equals("") Or TextBox3.Text.Equals("") Or TextBox4.Text.Equals("")) Then

                Dim title2 = "Invalid Values"
                MsgBox(" Form contains no values to save.", , title2)

            Else
                Dim ExistingData As New MyDatum

                Dim Serializer As New System.Xml.Serialization.XmlSerializer(GetType(MyDatum))

                If (File.Exists(My.Application.Info.DirectoryPath & "\TurbineData.xml" & Not EOF(1))) Then                     

                    Dim OpenStream As System.IO.FileStream = System.IO.File.Open(My.Application.Info.DirectoryPath & "\TurbineData.xml", IO.FileMode.Open)

                    ExistingData = Serializer.Deserialize(OpenStream)

                    OpenStream.Close()
                Else
                    Dim err05 = "File does not Exist"
                    MsgBox("Creating new XML file.", , err05)

                End If

                Dim NewData As New MyData

                TextBox1.Focus()
                NewData.DateStamp = CDate(Date.Today()) 'DateValue(Now)
                NewData.TimeStamp = CDate(TimeString())
                NewData.Gap = gaptext.Text
               
                ExistingData.Add(NewData)

                Dim SaveStream As System.IO.FileStream = System.IO.File.Open(My.Application.Info.DirectoryPath & "\TurbineData.xml", IO.FileMode.Create)

                Serializer.Serialize(SaveStream, ExistingData)

                SaveStream.Close()

                Dim title3 = "Save Alert"
                MsgBox("Data Saved to " & My.Application.Info.DirectoryPath & " \TurbineData.xml", , title3)

            End If

        Else
            Dim err05 = "File does not Exist"
            MsgBox("Creating new XML file.", , err05)

           Dim SaveStream As System.IO.FileStream = System.IO.File.Open(My.Application.Info.DirectoryPath & "\TurbineData.xml", IO.FileMode.Create)
            SaveStream.Close()
        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.

Tamir09
Light Poster
27 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Ah.... You are posting VB.NET code in the VB 4/5/6 Forum.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Oh I didnt know there was a difference...thought it was the same forum. I wonder if anyone has a solution to this?

Tamir09
Light Poster
27 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 
Private Sub RenameFile(ByRef fileNameFull As String)
    Dim fileI As FileInfo = New FileInfo(fileNameFull)
    If fileI.Exists Then
        Dim binReader As New BinaryReader(File.Open(fileNameFull, FileMode.Open))
        Dim fileClosed As Boolean = False
        Try
            ' read the first 4 bytes into a buffer to 
            ' determine if the file is empty.
            Dim testArray As Byte() = {0, 0, 0, 0}
            Dim count As Integer = binReader.Read(testArray, 0, 3)
            If count <> 0 Then 'file NOT empty
                ' Reset the position in the stream to zero.
                binReader.BaseStream.Seek(0, SeekOrigin.Begin)
                'do append stuff
            Else
                'file empty
            End If
        Catch ex As Exception
            If Not fileClosed Then
                binReader.Close()
                fileClosed = True
            End If
            MsgBox("Uncaught general Exception error. " + ex.GetType().Name + vbNewLine + ex.Message)
        Finally
            If Not fileClosed Then
                binReader.Close()
                fileClosed = True
            End If
        End Try
    End If
End Sub
edgar5
Light Poster
30 posts since Dec 2008
Reputation Points: 18
Solved Threads: 2
 
Dim reader() As String
reader = File.ReadAllLines(spath)
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.

martonx
Junior Poster in Training
51 posts since May 2008
Reputation Points: 10
Solved Threads: 8
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You