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) == ;
}
}
}
05-06 07:31