Visual C# を使用して File-Compare 関数を作成する

この記事では、Visual C# で File-Compare 関数を作成する方法について説明し、メソッドを説明するコード サンプルが含まれています。

元の製品バージョン: Visual C#
元の KB 番号: 320348

概要

この記事では、Microsoft .NET Framework クラス ライブラリ名前空間 を参照しますSystem.IO

この詳細な記事では、2 つのファイルを比較して内容が同じかどうかを確認する方法を示します。 この比較では、ファイル名、場所、日付、時刻、またはその他の属性ではなく、2 つのファイルの内容を調べます。

この機能は、さまざまなバージョンの Microsoft Windows と Microsoft MS-DOS、および一部の開発ツールに含まれる MS-DOS ベースの Fc.exe ユーティリティに似ています。

この記事で説明するサンプル コードでは、不一致が見つかるか、ファイルの末尾に到達するまで、バイト単位の比較を実行します。 このコードでは、比較の効率を高めるために、次の 2 つの簡単なチェックも実行します。

  • 両方のファイル参照が同じファイルを指している場合、2 つのファイルは等しい必要があります。
  • 2 つのファイルのサイズが同じでない場合、2 つのファイルは同じではありません。

サンプルを作成する

  1. 新しい Visual C# Windows アプリケーション プロジェクトを作成します。 既定では、Form1 が作成されます。

  2. フォームに 2 つのテキスト ボックス コントロールを追加します。

  3. フォームにコマンド ボタンを追加します。

  4. [表示] メニューの [コード] をクリックします。

  5. クラスに次 using のステートメントを Form1 追加します。

    using System.IO;
    
  6. 次のメソッドを Form1 クラスに追加します:

    // This method accepts two strings the represent two files to
    // compare. A return value of 0 indicates that the contents of the files
    // are the same. A return value of any other value indicates that the
    // files are not the same.
    private bool FileCompare(string file1, string file2)
    {
        int file1byte;
        int file2byte;
        FileStream fs1;
        FileStream fs2;
    
        // Determine if the same file was referenced two times.
        if (file1 == file2)
        {
            // Return true to indicate that the files are the same.
            return true;
        }
    
        // Open the two files.
        fs1 = new FileStream(file1, FileMode.Open);
        fs2 = new FileStream(file2, FileMode.Open);
    
        // Check the file sizes. If they are not the same, the files
        // are not the same.
        if (fs1.Length != fs2.Length)
        {
            // Close the file
            fs1.Close();
            fs2.Close();
    
            // Return false to indicate files are different
            return false;
        }
    
        // Read and compare a byte from each file until either a
        // non-matching set of bytes is found or until the end of
        // file1 is reached.
        do
        {
            // Read one byte from each file.
            file1byte = fs1.ReadByte();
            file2byte = fs2.ReadByte();
        }
        while ((file1byte == file2byte) && (file1byte != -1));
    
        // Close the files.
        fs1.Close();
        fs2.Close();
    
        // Return the success of the comparison. "file1byte" is
        // equal to "file2byte" at this point only if the files are
        // the same.
        return ((file1byte - file2byte) == 0);
    }
    
  7. コマンド ボタンのイベントに Click 次のコードを貼り付けます。

    private void button1_Click(object sender, System.EventArgs e)
    {
        // Compare the two files that referenced in the textbox controls.
        if (FileCompare(this.textBox1.Text, this.textBox2.Text))
        {
            MessageBox.Show("Files are equal.");
        }
        else
        {
            MessageBox.Show("Files are not equal.");
        }
    }
    
  8. サンプルを保存して実行します。

  9. テキスト ボックス内の 2 つのファイルへの完全なパスを指定し、コマンド ボタンをクリックします。

関連情報

詳細については、Microsoft Web サイト System.IO 名前空間に関するページを参照してください。