本分步指南介绍了如何比较两个文件,以查看其内容是否相同。此比较探讨了将两个文件的内容,但却不能在文件名称、 位置、 日期、 时间,或其他属性。
此功能,则类似于基于 ms-dos 的 Fc.exe 实用程序,包括与各种版本的 Microsoft Windows 和 Microsoft MS-DOS 和某些开发工具。
本文所述将示例代码执行逐字节比较,或直到找到不匹配,直到到达文件结尾。该代码还执行以下两个以提高效率的比较简单检查:
- 如果两个相同的文件的文件引用点,这两个文件必须相等。
- 如果两个大小的文件不同,这两个文件是不同的。
创建示例
- 启动 Visual Studio.net (2003) 或 Microsoft Visual Studio 2005。
- 在 文件 菜单上指向 新建,然后单击 项目。
- 在 项目类型 框中,单击 Visual c + + 项目,然后单击 模板 下的 空项目 (.NET)。命名该项目 FileCompare。
注意可视有关 Studio 的 2005 年在 项目类型 框中,单击 Visual c + +,然后单击 模板 下的 空项目。 - 在解决方案资源管理器中,用鼠标右键单击 FileCompare,指向 添加,然后再单击 添加新项。
- 添加新项 对话框中单击 模板 下的 页眉文件。在 名称 文本框中键入 Form1,然后单击 打开。显示 $ Form1.h 文件。
- Paste the following code in the Form1.h file:
#pragma once
#using <mscorlib.dll>
#using <system.windows.forms.dll>
#using <system.dll>
#using <system.drawing.dll>
namespace Compare
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System::IO;
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::TextBox * textBox1;
private: System::Windows::Forms::TextBox * textBox2;
private: System::Windows::Forms::Label * label1;
private: System::Windows::Forms::Label * label2;
private: System::Windows::Forms::Button * Compare;
private:
System::ComponentModel::Container * components;
void InitializeComponent(void)
{
this->textBox1 = new System::Windows::Forms::TextBox();
this->textBox2 = new System::Windows::Forms::TextBox();
this->label1 = new System::Windows::Forms::Label();
this->label2 = new System::Windows::Forms::Label();
this->Compare = new System::Windows::Forms::Button();
this->SuspendLayout();
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(32, 104);
this->textBox1->Name = S"textBox1";
this->textBox1->TabIndex = 0;
this->textBox1->Text = S"";
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(168, 104);
this->textBox2->Name = S"textBox2";
this->textBox2->TabIndex = 1;
this->textBox2->Text = S"";
//
// label1
//
this->label1->Location = System::Drawing::Point(32, 64);
this->label1->Name = S"label1";
this->label1->TabIndex = 2;
this->label1->Text = S"File1";
//
// label2
//
this->label2->Location = System::Drawing::Point(176, 64);
this->label2->Name = S"label2";
this->label2->TabIndex = 3;
this->label2->Text = S"File2";
//
// Compare
//
this->Compare->Location = System::Drawing::Point(96, 168);
this->Compare->Name = S"Compare";
this->Compare->TabIndex = 4;
this->Compare->Text = S"Compare";
this->Compare->Click += new System::EventHandler(this, button1_Click);
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(292, 273);
this->Controls->Add(this->Compare);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
this->Name = S"Form1";
this->Text = S"Form1";
this->ResumeLayout(false);
}
private:
System::Void button1_Click(System::Object * sender, System::EventArgs * e)
{
}
};
} 此代码创建一个包含两个文本框控件和一个命令按钮的窗体。
注意您必须添加公共语言运行库支持编译器选项 (/ clr:oldSyntax) 成功编译上面的代码示例中的 Visual c + + 2005年中。 在 Visual c + + 2005年中添加公共语言运行库支持编译器选项,请按照下列步骤操作: - 单击 项目,然后单击 $ <ProjectName> 属性。
注意<ProjectName> 是项目的名称的占位符。 - 展开 配置属性,然后单击 常规。
- 单击以选中 公共语言运行库支持、 $ 旧语法 (/ clr:oldSyntax) 在右窗格中 公共语言运行库支持 项目设置中,单击 应用,然后单击 确定。
有关公共语言运行库支持编译器选项,请访问下面的 Microsoft 网站: - 将下面的函数添加到 Form1 类:
// This method accepts two strings that 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 different.
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 (String::Equals(file1,file2))
{
// Return true to indicate that the files are the same.
return true;
}
// Open the two files.
try
{
fs1 = new FileStream(file1, FileMode::Open);
fs2 = new FileStream(file2, FileMode::Open);
// Check the file sizes. If the sizes are different, the files
// are different.
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();
}
catch(Exception *ex)
{
if(fs1)
{
fs1->Close();
}
if(fs2)
{
fs2->Close();
}
throw ex;
}
// 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);
}
- 将以下代码粘贴到 Form1 类中的 button1_Click 命令按钮的 Click 事件中:
try
{
if (FileCompare(this->textBox1->Text, this->textBox2->Text))
{
MessageBox::Show("Files are equal.");
}
else
{
MessageBox::Show("Files are not equal.");
}
}
catch(Exception *ex)
{
MessageBox::Show(ex->Message);
}
- 在解决方案资源管理器中,用鼠标右键单击 FileCompare,指向 添加,然后再单击 添加新项。
- 添加新项 对话框中单击 模板 下的 c + + 文件。在 名称 文本框中键入 File_Compare,然后单击 打开。显示 $ File_Compare.cpp 文件。
- 将以下代码粘贴到在 File_Compare.cpp file:
#include "form1.h"
#include <windows.h>
#include <tchar.h>
using namespace Compare;
- 将下面的 WinMain 函数添加到 File_Compare.cpp 文件:
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
} - 将以下代码粘贴到在 WinMain 函数中:
System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
Application::Run(new Form1());
return 0;
- 保存该示例。
运行示例
- 提供在文本框控件中的两个文件的完整路径。
- 单击 比较。
有关更多的信息,请访问下面的 Microsoft 开发人员网络 (MSDN) 的网站:
System.IO 命名空间
通过发现类的文件访问
文章编号: 816189 - 最后修改: 2007年11月26日 - 修订: 2.3
这篇文章中的信息适用于:
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET 2003 Standard Edition
- Microsoft Visual C++ .NET 2002 标准版
| kbmt kbhowtomaster kbhowto kbio KB816189 KbMtzh |
机器翻译注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误使用所引起的任何直接的, 或间接的可能的问题负责。
点击这里察看该文章的英文版:
816189
(http://support.microsoft.com/kb/816189/en-us/
)
Microsoft和/或其各供应商对于为任何目的而在本服务器上发布的文件及有关图形所含信息的适用性,不作任何声明。 所有该等文件及有关图形均"依样"提供,而不带任何性质的保证。Microsoft和/或其各供应商特此声明,对所有与该等信息有关的保证和条件不负任何责任,该等保证和条件包括关于适销性、符合特定用途、所有权和非侵权的所有默示保证和条件。在任何情况下,在由于使用或运行本服务器上的信息所引起的或与该等使用或运行有关的诉讼中,Microsoft和/或其各供应商就因丧失使用、数据或利润所导致的任何特别的、间接的、衍生性的损害或任何因使用而丧失所导致的之损害、数据或利润不负任何责任。