Keep file data consistent

Posted on 2009-05-01
Last Modified: 2012-05-06
I am adding image file data to database. On another pass I am checking the file name and file length of the file on disk against the equivalent data that I have stored in the database. The file name match up but the length does not. There is consistently a one byte difference between the number stored in the database and the length returned from the file on disk. Could anyone advise me as to where this extra byte comes from and how I might keep them consistent.
Question by:ruffone
    LVL 2

    Expert Comment

    I am throwing this out there for those who want to argue about it. Even though is the same file, it is located in a different place, even if it is on the same drive. This new location would make a change in the file structure, thus increasing or decreasing the file size. If the issue is with less than 10 bytes, I wouldn't sweat it unless you have written code that would require it to be exact.
    LVL 4

    Author Comment

    That is my issue. I am checking the folder for files that are not yet in the database. The file names are unique but I wanted to go that extra step and compare there lengths. Any work around to suggest
    LVL 29

    Expert Comment

    How are you copying the bytes from the file on disk into the databse?
    LVL 4

    Author Comment

    This is the function I am using to recover the data from the file
        Public Shared Function ReadFile(ByVal filename As String) As Byte()
            Using fs As New FileStream(filename, FileMode.Open, FileAccess.Read)
                Dim fileContents As Byte() = New Byte(fs.Length) {}
                fs.Read(fileContents, 0, Convert.ToInt32(fs.Length))
                Return fileContents
            End Using
        End Function

    Open in new window

    LVL 4

    Accepted Solution

    This fixed the problem
    Dim fileContents() As Byte = New Byte((fs.Length) - 1) {}

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now