Lm th? no ? t?o ra m?t ch?c nng t?p tin so snh trong Visual C#

D?ch tiu ? D?ch tiu ?
ID c?a bi: 320348 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi vi?t-by-step ny ch?ng t? lm th? no ? so snh hai t?p tin ? xem n?i dung c?a h? l nh nhau. So snh ny nh?n vo cc n?i dung c?a hai t?p tin, khng ph?i ? cc tn t?p tin, ?a i?m, ngy thng, th?i gian ho?c thu?c tnh khc.

Ch?c nng ny l tng t? nh MS-MS-DOS-d?a Fc.exe ti?n ch c ?c bao g?m cc phin b?n khc nhau c?a Microsoft Windows v Microsoft MS-DOS, v v?i m?t s? cng c? pht tri?n.

M?u m? ?c m t? trong bi vi?t ny th?c hi?n m?t so snh byte byte cho ?n khi n t?m th?y m?t khng ph h?p ho?c n ?t ?n cu?i cng c?a t?p tin. M? c?n th?c hi?n hai ki?m tra n gi?n ? tng hi?u qu? c?a vi?c so snh:
  • N?u c? hai t?p tin tham kh?o i?m vo cng m?t t?p tin, hai t?p tin ph?i ?c b?nh ?ng.
  • N?u kch th?c c?a hai t?p tin khng ph?i l nh v?y, hai t?p tin l khng gi?ng nhau.

? t?o ra cc m?u

  1. T?o m?t d? n m?i Visual C# c?a s? ?ng d?ng. Theo m?c ?nh, Form1 ?c t?o ra.
  2. Thm hai h?p ki?m sot m?u.
  3. Thm m?t nt l?nh m?u.
  4. Trn cc Xem tr?nh n, nh?p vo Ma.
  5. Thm d?i y B?NG CCH S? D?NG bo co cho cc Form1 l?p:
    using System.IO;
    					
  6. Thm cc phng php sau y ? cc Form1 l?p:
    // 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. Dn o?n m? sau trong cc Nh?p vo s? ki?n c?a nt l?nh:
    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. Lu v sau ch?y cc m?u.
  9. Cung c?p ?ng d?n ?y ? ?n hai t?p tin trong cc textboxes, v sau nh?p vo nt l?nh.

THAM KH?O

? bi?t thm chi ti?t, h?y truy c?p trang Web Microsoft sau y:
Khng gian tn System.io
(http://msdn2.Microsoft.com/en-US/Library/System.io (vs.71) .aspx)
Truy c?p t?p tin thng qua cc l?p h?c FileStream
(http://msdn2.Microsoft.com/en-US/Library/Aa903246 (VS.71) .aspx)

Thu?c tnh

ID c?a bi: 320348 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005 Express Edition
T? kha:
kbhowtomaster kbio kbmt KB320348 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:320348

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com