C# Large Files MD5 C# 获取大文件MD5


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net; 
using System.Threading;
using System.IO;
using System.Security.Cryptography;

namespace ConsoleApp348
{
    class Program
    {
        static List<String> MD5List = new List<string>();
        static string logFullPath = Directory.GetCurrentDirectory() + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
        static void Main(string[] args)
        {
            string dir = @"C:\Users\Fred\Downloads";
            GetFiles(dir);
            foreach(var md5 in MD5List)
            {
                Console.WriteLine(md5);
            }
            Console.ReadLine();
        }    
       

         

        static void LogMessage(string msg)
        {            
            lock(logFullPath)
            {
                using (StreamWriter logWriterStream = new StreamWriter(logFullPath, true, UTF8Encoding.UTF8))
                {
                    logWriterStream.WriteLine($"Log message :{msg},now is {DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
                }
            }           
        }
       
        static void GetFiles(string dir)
        {
            string[] allFiles = Directory.GetFiles(dir, "*", SearchOption.TopDirectoryOnly);
            Console.WriteLine($"There are totally {allFiles.Length} files in {dir}");
            if (allFiles != null && allFiles.Any())
            {
                for (int i = 0; i < allFiles.Length; i++)
                {
                    GenerateMD5ForLargeFiles(allFiles[i]);
                }
            }
        }
        static void GenerateMD5ForLargeFiles(string fileName)
        {
            StringBuilder stringBuilder = new StringBuilder();
            using (Stream readStream = File.OpenRead(fileName))
            {
                using (MD5 bigFileMD5 = MD5.Create())
                {
                    using (MD5CryptoServiceProvider cryptoServiceProvider = new MD5CryptoServiceProvider())
                    {
                        byte[] data = cryptoServiceProvider.ComputeHash(readStream);
                        for (int i = 0; i < data.Length; i++)
                        {
                            stringBuilder.Append(data[i].ToString("x2"));
                        }
                    }                                        
                }
            }
            MD5List.Add(stringBuilder.ToString());
            LogMessage(stringBuilder.ToString());
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net; 
using System.Threading;
using System.IO;
using System.Security.Cryptography;

namespace ConsoleApp348
{
    class Program
    {
        static List<String> MD5List = new List<string>();
        static void Main(string[] args)
        {
            string dir = @"C:\Users\Fred\Downloads";
            GetFiles(dir);
            
            if(MD5List.Any())
            {
                Parallel.ForEach(MD5List, x =>
                {
                    Console.WriteLine(x);
                });
            }
            Console.ReadLine();
        }

        static void GetFiles(string dir)
        {
            string[] allFiles = Directory.GetFiles(dir, "*", SearchOption.TopDirectoryOnly);
            Console.WriteLine($"There are totally {allFiles.Length} files in {dir}");
            if(allFiles!=null && allFiles.Any())
            {
                for(int i=0;i<allFiles.Length;i++)
                {
                    GenerateMD5ForLargeFiles(allFiles[i]);
                }
            }
        }
        static void GenerateMD5ForLargeFiles(string fileName)
        {
            StringBuilder stringBuilder = new StringBuilder();
            using (Stream readStream = File.OpenRead(fileName))
            {
                using (MD5 bigFileMD5 = MD5.Create())
                {
                    byte[] data = bigFileMD5.ComputeHash(readStream);
                    for(int i=0;i<data.Length;i++)
                    {
                        stringBuilder.Append(data[i].ToString("x2"));
                    }
                }
            }          
            MD5List.Add(stringBuilder.ToString());
        }
}

static void GenerateMD5ForLargeFiles(string fileName)
{
StringBuilder stringBuilder = new StringBuilder();
using (Stream readStream = File.OpenRead(fileName))
{
using (MD5 bigFileMD5 = MD5.Create())
{
byte[] data = bigFileMD5.ComputeHash(readStream);
for(int i=0;i<data.Length;i++)
{
stringBuilder.Append(data[i].ToString("x2"));
}
}
}
MD5List.Add(stringBuilder.ToString());
}

 

 

[ComVisible(true)]
public sealed class MD5CryptoServiceProvider : MD5

Computes the System.Security.Cryptography.MD5 hash value for the input data using
// the implementation provided by the cryptographic service provider (CSP). This
// class cannot be inherited.

posted @ 2019-05-22 00:34  FredGrit  阅读(773)  评论(0编辑  收藏  举报