public static string MDFile(string fileName)
        {
            
            FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read);
            byte[] array = new byte[fs.Length];
            fs.Read(array, 0, (int)fs.Length);
            byte[] digest = MD5Array(array);
            fs.Close();
            return ArrayToHexString(digest, false);
            
        }
上面这段程序我是在网上一些朋友的md5算法的程序中摘过来的,这段代码对于读大文件会有问题,因为把整个文件的大小都读进去了,所以对于大文件内存会吃光.请各位朋友请教

解决方案 »

  1.   

    这个  byte[] array = new byte[fs.Length];
                fs.Read(array, 0, (int)fs.Length);可以换成循环读,参考      byte[] bye = new byte[1024];
                int tt = stream.Read(bye, 0, length);
                //下面循环读取网络流并写进文件
                while (tt > 0)
                {
                    string ss = System.Text.Encoding.ASCII.GetString(bye);
                    int x = ss.IndexOf("<EOF>");
                    if (x != -1)
                    {
                        filestream.Write(bye, 0, x);
                        filestream.Flush();
                        break;
                    }
                    else
                    {
                        filestream.Write(bye, 0, tt);
                        filestream.Flush();
                    }
                    tt = stream.Read(bye, 0, length);
                }
      

  2.   

    md5并不是用来加密……,不过,.net中的md5可以直接使用stream方式来计算文件的md5值,不需要转换为byte[]来计算
      

  3.   

    嗯 直接用StreamReader打开传进去加密  不会吃内存