松鼠的博客

导航

c#获取文件md5值

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace MyHash
{
    
class md5
    {
        
/// <summary>
        
/// 实现对一个文件md5的读取,path为文件路径
        
/// </summary>
        
/// <param name="path"></param>
        
/// <returns></returns>
        public string md5_hash(string path)
        {
            
try
            {
                FileStream get_file 
= new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                System.Security.Cryptography.MD5CryptoServiceProvider get_md5 
= new System.Security.Cryptography.MD5CryptoServiceProvider();
                
byte[] hash_byte = get_md5.ComputeHash(get_file);
                
string resule = System.BitConverter.ToString(hash_byte);
                resule 
= resule.Replace("-""");
                
return resule;
            }
            
catch (Exception e)
            {
                
return e.ToString();
            }
        }
    }
}
 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;
 
public static void getFileMD5(string path)
{
    FileStream fs 
= new FileStream(path, FileMode.Open, FileAccess.Read);
    MD5CryptoServiceProvider md5 
= new MD5CryptoServiceProvider();
    
byte[] md5byte = md5.ComputeHash(fs);
    
int i, j;
    
foreach (byte b in md5byte)
    {
        i 
= Convert.ToInt32(b);
        j 
= i >> 4;
        Console.Write(Convert.ToString(j, 
16));
        j 
= ((i << 4& 0x00ff>> 4;
        Console.Write(Convert.ToString(j, 
16));
    }
    Console.ReadLine();
}

posted on 2008-10-23 09:16  Xproer-松鼠  阅读(693)  评论(1)    收藏  举报