Visual C#을 사용하여 File-Compare 함수 만들기

이 문서에서는 Visual C#에서 File-Compare 함수를 만드는 방법에 대한 정보를 제공하고 메서드를 설명하는 코드 샘플을 포함합니다.

원래 제품 버전: Visual C#
원래 KB 번호: 320348

요약

이 문서에서는 Microsoft .NET Framework 클래스 라이브러리 네임스페이스 를 참조합니다System.IO.

이 단계별 문서에서는 두 파일을 비교하여 내용이 동일한지 확인하는 방법을 보여 줍니다. 이 비교는 파일 이름, 위치, 날짜, 시간 또는 기타 특성이 아닌 두 파일의 내용을 살펴봅니다.

이 기능은 다양한 버전의 Microsoft Windows 및 Microsoft MS-DOS와 일부 개발 도구에 포함된 MS-DOS 기반 Fc.exe 유틸리티와 유사합니다.

이 문서에 설명된 샘플 코드는 일치하지 않거나 파일 끝에 도달할 때까지 바이트 바이트 비교를 수행합니다. 또한 이 코드는 두 가지 간단한 검사를 수행하여 비교의 효율성을 높입니다.

  • 두 파일 참조가 동일한 파일을 가리키는 경우 두 파일은 같아야 합니다.
  • 두 파일의 크기가 같지 않으면 두 파일은 동일하지 않습니다.

샘플 만들기

  1. 새 Visual C# Windows 애플리케이션 프로젝트를 만듭니다. 기본적으로 Form1이 만들어집니다.

  2. 양식에 두 개의 텍스트 상자 컨트롤을 추가합니다.

  3. 양식에 명령 단추를 추가합니다.

  4. 보기 메뉴에서 코드를 클릭합니다.

  5. 클래스에 다음 문을 Form1 추가합니다.using

    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. 텍스트 상자의 두 파일에 대한 전체 경로를 제공한 다음 명령 단추를 클릭합니다.

참조

자세한 내용은 Microsoft 웹 사이트 System.IO 네임스페이스를 참조하세요.