locked
File.Compare and can Microsoft make the System.Io.File class Inheritable please? Please vote for this feature!! RRS feed

  • Question

  • Hi All,

    I was looking at the methods in the SYSTEM.IO.FILE class and also discovered it is NotInheritable.

    Can Microsoft please add a File.Compare FUNCTION please or / and make it Inheritable?


    Please vote for this feature in Vb.Net 2010 versions.
    !

    I was trying to write one as an EXTENSION or a FUNCTION in another CLASS but SYSTEM.IO.FILE is NotInheritable.   :-(

    This FUNCTION will be similar to the File.Equals function only it can also return a STRING to say whether the files are the same, different or if one is longer than the other, as well as an array of the differences!!

    Well that is the intention at least!!

    This is how far I got after trying the code as an EXTENSION

    Hopefully you have the idea. :-)   ;-)

    Option

    Strict On
    Imports
    System.IO
    Public Class FileActions

    Inherits
    System.IO.File '<<<< It is NotInheritable!! :-(

    Public
    Structure DiffBytes

    Public
    FirstByte As Byte
    Public SecondByte As Byte

    End
    Structure

    Public
    Function Compare(ByVal File1PathAndFileName As String, ByVal File2PathAndFileName As String, Optional ByRef Message As String = "None", Optional ByRef FileDifferences() As DiffBytes = Nothing) As Boolean

    If
    File.Exists(File1PathAndFileName) And File.Exists(File2PathAndFileName) Then

    Dim
    FirstFileBytes() As Byte = File.ReadAllBytes(File1PathAndFileName)
    Dim SecondFileBytes() As Byte = File.ReadAllBytes(File2PathAndFileName)

    If
    FirstFileBytes.Length > SecondFileBytes.Length Then

    Message =
    "First file is longer."
    Return False

    ElseIf
    FirstFileBytes.Length < SecondFileBytes.Length Then

    Message =
    "Second file is longer."
    Return False

    ElseIf
    FirstFileBytes.Length = SecondFileBytes.Length Then

    Dim
    Differences(FirstFileBytes.Length - 1) As DiffBytes

    For
    index = 0 To FirstFileBytes.GetUpperBound(0)

    If
    FirstFileBytes(index) <> SecondFileBytes(index) Then

    Message =
    "The files have differences!!"
    Differences(index).FirstByte = FirstFileBytes(index)
    Differences(index).SecondByte = SecondFileBytes(index)
    End If

    Next

    FileDifferences =
    CType(Differences.Clone, DiffBytes())

    If
    Message = "The files have differences!!" Then
    Return False
    Else
    Message = "Files are the same."
    Return True
    End If

    End
    If

    End
    If

    End
    Function

    End
    Class




    Regards,

    John
    Monday, April 6, 2009 12:18 AM

All replies

  • Hi again everyone,

    I am about to try my code in the 1st post above.

    Obviously without the IMPORTS line of code.

    File.Differences could be another PUBLIC function name that could be called if two files are in any way different.

    It could return an array where the bytes are different and the array would be the length of the shorter file if one file is shorter than the other.

    Just thought I would add this idea too!!

    ;-)   :-)


    Regards,

    John
    Monday, April 6, 2009 12:42 AM
  • Thanks for the feedback, have you entered this in connect so others can vote?

    -Euan
    http://blogs.msdn.com/euanga
    Friday, April 17, 2009 12:36 AM
  • Thanks for the feedback, have you entered this in connect so others can vote?

    -Euan
    http://blogs.msdn.com/euanga
    Hi,

    No but I will now you have mentioned it. :-)

    Okay you can vote for it here too.>>

    https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=433611


    Regards,

    John

    Sunday, April 19, 2009 8:38 PM