2013-08-23 59 views
0

我有函數來檢查這兩個文件是否相似。所以我做的一件事就是比較文件的大小。我正在尋找一種比較文件內容並獲得匹配百分比的方法。根據百分比值,我可以決定它們是否大致相等?比較任何文件類型的兩個文件的內容

如果是文本文件,我正在閱讀文本並計算差異。但如果它是一個excel或任何其他文件有這樣的圖像?

+0

你能定義你的意思是「相似」嗎? – lurker

+0

如果你打開一個Excel文件(或一個pdf或任何其他文件類型與複雜/加密的內容),你會得到一堆或怪異的符號沒有意義。您可以將這些想法僅應用於適合(直接)轉換爲.txt文件的文件。或者,從邏輯上講,依靠相應的API(一個用於Excel,另一個用於pdf,另一個用於等)。 – varocarbas

+0

這裏有一些很好的問題http://stackoverflow.com/questions/9065536/text-comparison-algorithm about comparison兩個文本文件。這將是非常困難的比較二進制文件,因爲你永遠不會知道如何處理信息。 –

回答

0

你可以嘗試這樣的事情,然後根據2個文件的比較做出決定。

Imports System.IO 

Public Class FileSizeChecker 

Public Sub FileChecker() 

    Dim info As New FileInfo("test.txt") 

    ' Get length of the file. 
    Dim length As Long = info.Length 

    ' Add more characters to the file. 
    File.AppendAllText("test.txt", " More characters.") 

    ' Get another file info. 
    ' ... Then get the length. 
    Dim info2 As New FileInfo("test.txt") 
    Dim length2 As Long = info2.Length 

    ' Show how the size changed. 
    Console.WriteLine("Before and after: {0}, {1}", length, length2) 
    Console.WriteLine("Size increase: {0}", length2 - length) 

End Sub