生成指定数量的随机文件,但为了方便判断其正确性,需要每发50个就暂停检查一次,请大家帮忙看看

lisa9342 2012-05-02 09:42:19
/// <summary>
/// 生成随机文件
/// </summary>
/// <param name="args"></param>
public void filesCreat(string[] args)
{
uint cnt = 0;
string ext = ".bin";
uint fileNum = Convert.ToUInt32(txtFileNumber.Text);
for (cnt = 0; cnt < fileNum; ++cnt)
{
string newName = BuildFileName(txtFilenamePrefix.Text, ext, cnt);
string test1 = Convert.ToString(Utils._creatPath + "\\" + newName);
Console.WriteLine(newName);

//每发送50个文件暂停一次,检查接收目录是否收完,
//计算两者的MD5值存入集合进行比较,若都正确则继续发文件,
//直到发完用户指定的数量,否则,则提示错误
if (fileNum%50==0)
{

}


r.NextBytes(buffer);
using (System.IO.FileStream fs = new System.IO.FileStream(test1, System.IO.FileMode.Create))
{
int maxSize = Convert.ToInt32(txtFileSize.Text) * 1024;
if (!checkBoxSize.Checked)
size = r.Next(1, maxSize);
else
size = maxSize;
fs.Write(buffer, 0, size);
}
}
}
...全文
87 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lisa9342 2012-05-02
  • 打赏
  • 举报
回复
快来人哪!怎么这么没有同情心哪!
lisa9342 2012-05-02
  • 打赏
  • 举报
回复
求求各位,帮忙看看想想啊!怎么没人来呢?
lisa9342 2012-05-02
  • 打赏
  • 举报
回复
其实,我想问的重点是:怎么暂停发送??检查完条件之后再接着发的问题!谢谢!
devmiao 2012-05-02
  • 打赏
  • 举报
回复
c#获取文件md5值


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace MyHash
{
class md5
{
/// <summary>
/// 实现对一个文件md5的读取,path为文件路径
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public string md5_hash(string path)
{
try
{
FileStream get_file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hash_byte = get_md5.ComputeHash(get_file);
string resule = System.BitConverter.ToString(hash_byte);
resule = resule.Replace("-", "");
return resule;
}
catch (Exception e)
{

return e.ToString();

}



}
}
}






===================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;


public static void getFileMD5(string path)
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] md5byte = md5.ComputeHash(fs);
int i, j;
foreach (byte b in md5byte)
{
i = Convert.ToInt32(b);
j = i >> 4;
Console.Write(Convert.ToString(j, 16));
j = ((i << 4) & 0x00ff) >> 4;
Console.Write(Convert.ToString(j, 16));
}
Console.ReadLine();

}







=================================================

public enum HashType
{
MD5 = 0,
SHA1=1
}

public class Md5Checker
{

/// <summary>
/// 计算文件的 MD5 值
/// </summary>
/// <param name="fileName">要计算 MD5 值的文件名和路径</param>
/// <returns>MD5 值16进制字符串</returns>
public static string MD5File(string fileName)
{
return HashFile(fileName, HashType.MD5);
}


/// <summary>
/// 计算文件的 SHA1 值
/// </summary>
/// <param name="fileName">要计算 SHA1 值的文件名和路径</param>
/// <returns>SHA1 值16进制字符串</returns>
public static string SHA1File(string fileName)
{
return HashFile(fileName, HashType.SHA1);
}


/// <summary>
/// 计算文件的哈希值
/// </summary>
/// <param name="fileName">要计算哈希值的文件名和路径</param>
/// <param name="algName">算法:sha1,md5</param>
/// <returns>哈希值16进制字符串</returns>
public static string HashFile(string fileName, HashType hashType)
{
if (!System.IO.File.Exists(fileName))
return string.Empty;

FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
byte[] hashBytes = HashData(fs, hashType);
fs.Close();
return ByteArrayToHexString(hashBytes);
}


/// <summary>
/// 计算哈希值
/// </summary>
/// <param name="stream">要计算哈希值的 Stream</param>
/// <param name="hashType">算法:sha1,md5</param>
/// <returns>哈希值字节数组</returns>
public static byte[] HashData(Stream stream, HashType hashType)
{
HashAlgorithm algorithm = null;
switch (hashType)
{
case HashType.MD5:
algorithm = MD5.Create();
break;
case HashType.SHA1:
algorithm = SHA1.Create();
break;
default:
break;
}
return algorithm.ComputeHash(stream);
}


/// <summary>
/// 字节数组转换为16进制表示的字符串
/// </summary>
public static string ByteArrayToHexString(byte[] buf)
{
int iLen = 0;

// 通过反射获取 MachineKeySection 中的 ByteArrayToHexString 方法,该方法用于将字节数组转换为16进制表示的字符串。
Type type = typeof(System.Web.Configuration.MachineKeySection);
MethodInfo byteArrayToHexString = type.GetMethod("ByteArrayToHexString", BindingFlags.Static | BindingFlags.NonPublic);

// 字节数组转换为16进制表示的字符串
return (string)byteArrayToHexString.Invoke(null, new object[] { buf, iLen });
}


}

lisa9342 2012-05-02
  • 打赏
  • 举报
回复
计算MD5、存入集合的方法都已写好:
//把发送文件的MD5值存入TestDic1
public void TestDic1()
{

StringDictionary sdic = new StringDictionary();
sdic.Add("a", "1");
sdic.Add("ab", "1");
sdic.Add("abc", "1");
sdic.Add("abcd", "1");
sdic.Add("abcde", "1");
foreach (string key in sdic.Keys)
{
string value = sdic[key];
Console.WriteLine("key:{0}, value:{1}", key, value);
}
}

//把接收文件的MD5值存入TestDic2
public void TestDic2()
{
Dictionary<string, uint> dic = new Dictionary<string, uint>();
dic.Add("a", 1);
dic.Add("ab", 1);
dic.Add("abc", 1);
dic.Add("abcd", 1);
dic.Add("abcde", 1);
foreach (string key in dic.Keys)
{
uint val = dic[key];
Console.WriteLine("key:{0}, value:{1}", key, val);
}
}


public byte[] MD5Compute(byte fileContent)
{
MD5 md5Hasher = MD5.Create();
byte[] md5 = md5Hasher.ComputeHash(buffer);
return md5;
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧