请教C#自己取自身的文件MD5值
1个回答
展开全部
您好,这样:
private string GetFileMd5Code(string filePath)
{
StringBuilder builder = new StringBuilder();
using(var md5=new MD5CryptoServiceProvider())
{
File.Copy(filePath,filePath+"e");//复制一份,防止占用
using(FileStream fs=new FileStream(filePath+"e",FileMode.Open))
{
byte[] bt=md5.ComputeHash(fs);
for (int i = 0; i < bt.Length; i++)
{
builder.Append(bt[i].ToString("x2"));
}
}
File.Delete(filePath+"e");//删除复制的文件,这里没处理异常等....
}
return builder.ToString();
}
private string GetFileMd5Code(string filePath)
{
StringBuilder builder = new StringBuilder();
using(var md5=new MD5CryptoServiceProvider())
{
File.Copy(filePath,filePath+"e");//复制一份,防止占用
using(FileStream fs=new FileStream(filePath+"e",FileMode.Open))
{
byte[] bt=md5.ComputeHash(fs);
for (int i = 0; i < bt.Length; i++)
{
builder.Append(bt[i].ToString("x2"));
}
}
File.Delete(filePath+"e");//删除复制的文件,这里没处理异常等....
}
return builder.ToString();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询