首页 技术 正文
技术 2022年11月14日
0 收藏 599 点赞 2,456 浏览 4298 个字
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace FileStrem大文件分割复制
{
public partial class Form1 : Form
{
private int WriterByetNub = ;//100M复制速度
//源目标
private FileStream FileToRead;
//复制到文件
private FileStream FileToWrite;
//保存文件的地址
private string SaveFile_Add;
//源文件的名字
private string File_Add;
//设置正常写入字节
private Byte[] byteToWrite;
//设置剩余写入字节
private Byte[] byteToLastWrite;
//循环次数
private long WriteTimes;
//循环后的剩余字节
private int L_Size; public Form1()
{
InitializeComponent();
}
//设置委托
private delegate void OpenFile(); private void Cpy()
{
try
{
label_Add.Text = "源地址"; label_Cpy_Add.Text = "复制到"; label_Cpy_Lc.Text = "复制进程:"; label_Write.Text = "已经写入"; label_FileSize.Text = "源文件总大小";
//文件选取
OpenFileDialog openfileDialog = new OpenFileDialog();
//show文件选取器
openfileDialog.ShowDialog(); File_Add = openfileDialog.FileName; label_Add.Text += ":" + File_Add; //保存地址选取
FolderBrowserDialog savefileDialog = new FolderBrowserDialog(); savefileDialog.ShowDialog(); SaveFile_Add = savefileDialog.SelectedPath; label_Cpy_Add.Text += ":" + SaveFile_Add + File_Add; FileToRead = new FileStream(File_Add, FileMode.Open, FileAccess.Read); FileToWrite = new FileStream(@SaveFile_Add + "\\" + openfileDialog.SafeFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); label_FileSize.Text = "源文件总大小"+(FileToRead.Length/).ToString()+"KB";
if (FileToRead.Length > WriterByetNub)
//设置写入字节数组
{
byteToWrite = new byte[WriterByetNub];
//循环次数
WriteTimes = FileToRead.Length / WriterByetNub;
//多次循环后剩余字节
L_Size = Convert.ToInt32(FileToRead.Length % WriterByetNub);
//多次循环后字节数组
byteToLastWrite = new byte[L_Size]; for (long i = ; i <= WriteTimes; i++)
{
//读源文件
FileToRead.Read(byteToWrite, , WriterByetNub); //写数据到目标文件
FileToWrite.Write(byteToWrite, , WriterByetNub); //设置进度条的值
progressBar.Value = Convert.ToInt32(i * / WriteTimes); Application.DoEvents(); //设置Lable上的进度值
label_Cpy_Lc.Text = "复制进程:" + Convert.ToInt32((i * ) / WriteTimes).ToString() + "%"; //设置写入值
label_Write.Text = "已写入" + (FileToRead.Position / ).ToString() + "KB";
} //剩余字节的读和写
if (L_Size != )
{
FileToRead.Read(byteToLastWrite, , L_Size); FileToWrite.Write(byteToLastWrite, , L_Size);
}
}
else
{
progressBar.Maximum =(int) FileToRead.Length;
byteToWrite = new byte[FileToRead.Length];
FileToRead.Read(byteToWrite, , (int)FileToRead.Length);
label_Cpy_Lc.Text = "复制进程:" + Convert.ToInt32(FileToRead.Position/FileToRead.Length*).ToString() + "%"; //设置写入值
label_Write.Text = "已写入" + (FileToRead.Position / ).ToString() + "KB";
progressBar.Value =(int )FileToRead.Position;
FileToWrite.Write(byteToWrite, , (int)FileToRead.Length);
}
FileToRead.Flush(); FileToWrite.Flush(); FileToRead.Close(); FileToWrite.Close(); MessageBox.Show("复制完成");
}
catch(Exception ex) {
FileToRead.Flush(); FileToWrite.Flush(); FileToRead.Close(); FileToWrite.Close(); MessageBox.Show(ex.ToString()); }
} private void openFileBtn_Click(object sender, EventArgs e)
{
OpenFile getFile = new OpenFile(Cpy);
this.Invoke(getFile);
} private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
}
}
private void button3_Click(object sender, EventArgs e)
{
FolderBrowserDialog f = new FolderBrowserDialog();
if (f.ShowDialog() == DialogResult.OK)
{
textBox2.Text = f.SelectedPath +@"\"+Path.GetFileName(textBox1.Text.Trim());
}
} private void button1_Click(object sender, EventArgs e)
{
ApiCopyFile.DoCopy(textBox1.Text.Trim(), textBox2.Text.Trim());
} }
public class ApiCopyFile
{
private const int FO_COPY = 0x0002;
private const int FOF_ALLOWUNDO = 0x00044;
//显示进度条 0x00044 // 不显示一个进度对话框 0x0100 显示进度对话框单不显示进度条 0x0002显示进度条和对话框
private const int FOF_SILENT = 0x0002;//0x0100;
//
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = )]
public struct SHFILEOPSTRUCT
{
public IntPtr hwnd;
[MarshalAs(UnmanagedType.U4)]
public int wFunc;
public string pFrom;
public string pTo;
public short fFlags;
[MarshalAs(UnmanagedType.Bool)]
public bool fAnyOperationsAborted;
public IntPtr hNameMappings;
public string lpszProgressTitle;
}
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
public static bool DoCopy(string strSource, string strTarget)
{
SHFILEOPSTRUCT fileop = new SHFILEOPSTRUCT();
fileop.wFunc = FO_COPY;
fileop.pFrom = strSource;
fileop.lpszProgressTitle = "复制大文件";
fileop.pTo = strTarget;
//fileop.fFlags = FOF_ALLOWUNDO;
fileop.fFlags = FOF_SILENT; return SHFileOperation(ref fileop) == ;
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297