| | |
How to check if a file is empty
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2009
Posts: 27
Reputation:
Solved Threads: 0
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:
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.
VB.NET Syntax (Toggle Plain Text)
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.
•
•
Join Date: Dec 2008
Posts: 28
Reputation:
Solved Threads: 2
VB.NET Syntax (Toggle Plain Text)
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
-Ed
MS Vista Ultimate 64-bit; Visual Studio 2008 & 2010 beta; Visual Basic; Visual C++
Asus P5E3 Deluxe w/ Intel E6850, 8 GB OCZ DDR3 PC3-12800, SAPPHIRE HD3870 GPU
MS Vista Ultimate 64-bit; Visual Studio 2008 & 2010 beta; Visual Basic; Visual C++
Asus P5E3 Deluxe w/ Intel E6850, 8 GB OCZ DDR3 PC3-12800, SAPPHIRE HD3870 GPU
•
•
Join Date: May 2008
Posts: 51
Reputation:
Solved Threads: 8
VB.NET Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- writting and reading a file (Java)
- open a empty CSV file (Python)
- file upload code.....need attention (PHP)
- If stdin is empty? (Python)
- Anagrams (C++)
- usinfg c++, how would you check if a cd-rw is blank... (C++)
- Here is the doc file for the assignment (C++)
- ofstream problem (C)
- system crashes,gets slow, and network gets down,Hijacklog file pvded (Viruses, Spyware and other Nasties)
Other Threads in the VB.NET Forum
- Previous Thread: Trying to create CSV file using VB.NET
- Next Thread: Save & Update
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net .net2008 2005 2008 access account application array arrays basic beginner browser button buttons center checkbox client code convert cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic eclipse excel fade filter forms ftp generatetags gridview html images inline input insert intel internet lib listview mobile monitor net objects open panel passingparameters pdf picturebox port position print printing problem read remove save searchvb.net select serial settings shutdown soap sorting sqlserver survey table temperature textbox timer timespan transparency trim update user validation vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web webbrowser winforms wpf wrapingcode year






