C#分块拷贝大文件
2024-10-20 05:41:09
//定义源文件和目标文件,绝对路径
public static string source = @"E:\C#\C#编程语言详解.pdf";
//2014-6-10 Training
//拷贝大文件,分块拷贝
AddBigFile(source);
/// <summary>
/// 拷贝大文件
/// </summary>
/// <param name="source">原绝对路径</param>
private static void AddBigFile(string source)
{
int i = ;
using (FileStream fsRead = new FileStream(source, FileMode.Open))
{
byte[] byts = new byte[ * * ];
while (true)
{
int r = fsRead.Read(byts, , byts.Length); if (r <= )
{
Console.WriteLine("----End----");
break;
}
DataBaseController.AddFiles(source, i, byts);
Console.WriteLine("FileName:" + source + "第 " + i + "个" + "大小:" + byts.Length);
i++;
}
}
} DataBaseController.AddFiles函数是拿到二进制数据,插入数据库操作: public static class DataBaseController
{
public static readonly string connstr = "Data Source=.;Initial Catalog=AddFile;Persist Security Info=True;User ID=sa;Password=sa";
public static string Tosource = @"E:\C#\Copy123C#编程语言详解.pdf"; public static void AddFiles(string FileName,int Code,byte[] Data)
{
string sql = @"insert into FileBlock(FileName, Code, Data) values(@FileName, @Code, @Data)";
DateTime time = DateTime.Now;
SqlParameter[] pars = {
new SqlParameter("@FileName",FileName),
new SqlParameter("@Code",Code),
new SqlParameter("@Data",Data) };
int count = SqlHelper.ExecuteNonQuery(connstr, CommandType.Text, sql, pars);
} public static void GetFilesByName(string FileName)
{
string sql = @"select * from FileBlock where FileName = @FileName";
DateTime time = DateTime.Now;
SqlParameter[] pars = {
new SqlParameter("@FileName",FileName),
};
List<FileBlock> list = new List<FileBlock>();
using (SqlDataReader reader = SqlHelper.ExecuteReader(connstr, CommandType.Text, sql, pars))
{
list = Application.Data2Objects<FileBlock>(reader);
} //循环查出文件的多个块,拼接写在一个文件中
FileStream fs = new FileStream(Tosource,FileMode.Create);
foreach (var item in list)
{
fs.Write(item.Data,,item.Data.Length);
} //byte[] data = new byte[fs.Length];
//fs.Read(data,0,fs.Length);
//fs.Seek(0,SeekOrigin.Begin);
// return data;
}
} public class FileBlock
{
public int ID { get; set; }
public string FileName { get; set; }
public int Code { get; set; }
public byte[] Data { get; set; }
}
最新文章
- json使用
- Android点击效果
- LCS(Longest Common Subsequence 最长公共子序列)
- 有关git的换行符的处理问题
- Bootstrap日期和时间表单组件运用兼容ie8
- 曝光卖假币的店铺和旺旺ID
- ruby 模块 的引入
- C++PRIMER 阅读笔记 第三章
- Udacity并行计算课程笔记-The GPU Programming Model
- 使用kafka connect,将数据批量写到hdfs完整过程
- java集合HashMap、HashTable、HashSet详解
- IntelliJ IDEA 最新激活码
- listview--Java泛型应用之打造Android万能ViewHolder-超简洁写法
- jumpserver笔记
- 应用间共享文件 FileProvider
- 步步为营-10-string的简单操作
- squid 快速配置
- github优秀前端项目分享(转)
- 大数据开发实战:Hadoop数据仓库开发实战
- python学习笔记(二十九)为什么python的多线程不能利用多核CPU
热门文章
- Java爬虫系列一:写在开始前
- Eclipse下tomcat输出路径配置
- jmeter beanshell Typed variable declaration : Object constructor错误
- POJ2758 Checking the Text 哈希
- P2062 分队问题(贪心orDP)
- myeclipse非正常关闭处理办法
- Linux中ext2文件系统的结构
- HDU 5505——GT and numbers——————【素数】
- 冷笑话,idea 按删除键就是undo?
- asp.net 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction