码迷,mamicode.com
首页 > 其他好文 > 详细

C#统计目录中文件MD5值

时间:2014-09-13 17:05:25      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   io   os   ar   for   文件   数据   

1. [代码]统计目录中文件MD5值     

using System.IO;
using System.Security.Cryptography;
using System.Collections;
using System.Windows.Forms;
 IDataObject iData = Clipboard.GetDataObject();
   if (iData.GetDataPresent(DataFormats.Text))
   {
string filepath=(string)iData.GetData(DataFormats.Text);
StringBuilder sbMD5= new StringBuilder(1024);
string filepath=@"C:\tmp";
DirectoryInfo dir = new DirectoryInfo((filepath.LastIndexOf(Path.DirectorySeparatorChar) == filepath.Length - 1) ? filepath : filepath + Path.DirectorySeparatorChar);
FileSystemInfo[] fileArr = dir.GetFileSystemInfos();
Queue<FileSystemInfo> Folders = new Queue<FileSystemInfo>(dir.GetFileSystemInfos());
while (Folders.Count > 0)
{
    FileSystemInfo atom = Folders.Dequeue();
    FileInfo f = atom as FileInfo;
    if (f == null)
    {
        DirectoryInfo d = atom as DirectoryInfo;
        foreach (FileSystemInfo fi in d.GetFileSystemInfos())
            Folders.Enqueue(fi);
    }
    else
    {
        FileStream fs = new FileStream(f.FullName,FileMode.Open,FileAccess.Read);
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        byte [] md5byte = md5.ComputeHash(fs);
        int i,j;
        StringBuilder sb = new StringBuilder(16);
        foreach (byte b in md5byte)
        {
            i = Convert.ToInt32(b);
           j = i >> 4;
           sb.Append(Convert.ToString(j,16));
           j = ((i << 4) & 0x00ff) >> 4;
           sb.Append(Convert.ToString(j,16));
        }
        sbMD5.Append(f.FullName);
        sbMD5.Append("\t");
        sbMD5.Append(sb.ToString());
        sbMD5.Append("\r\n");
    }
}
Clipboard.SetText(sbMD5.ToString());
   }
   else
    MessageBox.Show("目前剪贴板中数据不可转换为文本","错误");
2. [代码][C#]代码     
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Security.Cryptography;
using System.Collections;
using System.Windows.Forms;
 http://www.huiyi8.com/gongzuozongjie/?
static void listFiles(string rootDirectory,string searchPattern)
{工作总结
var files=from fileName in Directory.GetFiles(
rootDirectory,searchPattern)
select new
{
Name=fileName,
LastWriteTime=File.GetLastWriteTime(fileName)
};
StringBuilder sbMD5= new StringBuilder(1024);
foreach(var file in files)
{
        FileStream fs = new FileStream(file.Name,FileMode.Open,FileAccess.Read);
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        byte [] md5byte = md5.ComputeHash(fs);
        StringBuilder sb = new StringBuilder(16);
        foreach (byte b in md5byte)
        {
            int i = Convert.ToInt32(b);
           int j = i >> 4;
           sb.Append(Convert.ToString(j,16));
           j = ((i << 4) & 0x00ff) >> 4;
           sb.Append(Convert.ToString(j,16));
        }
        sbMD5.Append(f.Name);
        sbMD5.Append("\t");
        sbMD5.Append(f.LastWriteTime);
        sbMD5.Append("\t");
        sbMD5.Append(sb.ToString());
        sbMD5.Append("\r\n");
}
Clipboard.SetText(sbMD5.ToString());
}
 
 
 IDataObject iData = Clipboard.GetDataObject();
   if (iData.GetDataPresent(DataFormats.Text))
   {
string filepath=(string)iData.GetData(DataFormats.Text);
listFiles(filepath,"*");
}

C#统计目录中文件MD5值

标签:style   http   color   io   os   ar   for   文件   数据   

原文地址:http://www.cnblogs.com/xkzy/p/3969914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!