RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
c#如何给文件计算MD5-创新互联

在项目中,当我们需要把数据文件上传到ftp时,为了保证数据的完整性,我们会计算数据文件的MD5,并把MD5文件和数据文件一同上传。

创新互联从2013年成立,先为海棠等服务建站,海棠等地企业,进行企业商务咨询服务。为海棠企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

那么如何产生数据文件的MD5呢?

我们可以通过System.Security.Cryptography.MD5类来

private void WriteMD5(string zipFile, string ctrlFile)
        {
            // Add hash code to ctrl file
            byte[] validHash = GenerateMD5Hash(zipFile);
            // make sure that we add hash value in a new line
            bool addNewLine = false;
            using (StreamReader sr = new StreamReader(ctrlFile))
            {
                string[] lines = sr.ReadToEnd().Split(new char[] { '\n' });
                if (lines.Length == 1) addNewLine = true;
            }

            // ready to put the control file, so add the hash string to it
            using (StreamWriter sw = new StreamWriter(ctrlFile, true))
            {
                if (addNewLine) sw.WriteLine("");
                sw.WriteLine(BitConverter.ToString(validHash).Replace("-", ""));
            }
        }

private byte[] GenerateMD5Hash(string localFileName)
        {
            byte[] localHash = null;
            using (MD5 md5Hash = MD5.Create())
            {
                using (FileStream localFile = File.OpenRead(localFileName))
                {
                    localHash = md5Hash.ComputeHash(localFile);
                }
            }
            return localHash;
        }

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


新闻标题:c#如何给文件计算MD5-创新互联
分享URL:http://sczitong.cn/article/cdiipo.html