/// <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);
}
}
}
/// 生成随机文件
/// </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);
}
}
}
解决方案 »
- c# datatable批量插入oracle
- 探讨魔兽dota改建原理
- 如何将DataTable赋予SQL数据库中结构相同的表??[紧急求助]
- 使用 Webclient 上传文件到服务器,总报405错误....
- 如何将下面的C#代码转换成VB.NET代码?急
- 求.net sqlclient 和 oldeb data provider的性能比较文章
- 请问在C#中如何实现获得某目录下的子目录和文件列表?
- 斑马打印机的指令如何跟变量 ?八百里加急 在线等
- C#可否使用全局钩子技术?我的程序需要热键呼出!
- 关于datagrid更新数据库的问题(我是新手,大家帮帮我!)
- 请教一正则表达式语句
- C# dataset!!!!
//把发送文件的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;
}
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 });
}
}