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.

Recommended Answers

All 6 Replies

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

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

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
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.

How do i do it if I want to check if a its a csv file?

Hi Zed_1 welcome. :)
A cvs file is the same as a text file.
But in the future, it is better to pose your question in a new thread, perhaps referring to this already solved and answered thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.