File-Compare işlevi oluşturmak için Visual C# kullanma

Bu makale, Visual C# içinde File-Compare işlevi oluşturma hakkında bilgi sağlar ve yöntemleri açıklamak için bir kod örneği içerir.

Orijinal ürün sürümü: Visual C#
Özgün KB numarası: 320348

Özet

Bu makale, Microsoft .NET Framework Sınıf Kitaplığı ad alanını System.IOifade eder.

Bu adım adım makale, içeriklerinin aynı olup olmadığını görmek için iki dosyanın nasıl karşılaştırılıp karşılaştırılamadığını gösterir. Bu karşılaştırma dosya adlarında, konumlarında, tarihlerinde, saatlerinde veya diğer özniteliklerde değil, iki dosyanın içeriğine bakar.

Bu işlev, Microsoft Windows ve Microsoft MS-DOS'un çeşitli sürümlerine ve bazı geliştirme araçlarına dahil olan MS-DOS tabanlı Fc.exe yardımcı programıyla benzerdir.

Bu makalede açıklanan örnek kod, bir uyuşmazlık bulana veya dosyanın sonuna ulaşana kadar bayt bayt karşılaştırması gerçekleştirir. Kod ayrıca karşılaştırmanın verimliliğini artırmak için iki basit denetim gerçekleştirir:

  • Her iki dosya başvurusu da aynı dosyaya işaret ederse, iki dosya eşit olmalıdır.
  • İki dosyanın boyutu aynı değilse, iki dosya aynı değildir.

Örneği oluşturma

  1. Yeni bir Visual C# Windows Uygulaması projesi oluşturun. Varsayılan olarak Form1 oluşturulur.

  2. Forma iki metin kutusu denetimi ekleyin.

  3. Forma bir komut düğmesi ekleyin.

  4. Görünüm menüsünde Kod'a tıklayın.

  5. Sınıfına aşağıdaki using deyimi Form1 ekleyin:

    using System.IO;
    
  6. Sınıfına Form1 aşağıdaki yöntemi ekleyin:

    // 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. Komut düğmesinin olayına Click aşağıdaki kodu yapıştırın:

    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. Örneği kaydedin ve çalıştırın.

  9. Metin kutularındaki iki dosyanın tam yollarını sağlayın ve ardından komut düğmesine tıklayın.

Başvurular

Daha fazla bilgi için Ad Alanı System.IO Microsoft Web sitelerini ziyaret edin.