C#文件流实现文件复制
来源:互联网 发布:能备案的域名后缀 编辑:程序博客网 时间:2024/05/29 18:10
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
if (op.ShowDialog()==DialogResult.OK)
{
textBox1.Text = op.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog sf = new SaveFileDialog();
if (sf.ShowDialog()==DialogResult.OK)
{
textBox2.Text = sf.FileName;
}
}
private void btnStart_Click(object sender, EventArgs e)
{
//创建读取流
FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read, FileShare.Read);
FileInfo fi = new FileInfo(textBox1.Text);
byte[] buffer=new byte[1024];
long count= 0;
//创建写入流
FileStream fsm = new FileStream(textBox2.Text, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
while (true)
{
//读取数组
int len= fs.Read(buffer, 0, buffer.Length);
//每次以读取的长度写入
fsm.Write(buffer, 0, len);
count += len;
long p = (count * 100) / fi.Length;
progressBar1.Value = (int)p;
//如果没有读取到内容,跳出循环
if (len==0)
{
break;
}
}
fs.Close();
fsm.Close();
}
private void timer1_Tick(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
//读取,跳到多少个字节后读取
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog()==DialogResult.OK)
{
string fileName = ofd.FileName;
//读取fileName对应的文件10个字节后的内容
FileStream fs = new FileStream(fileName, FileMode.Open);
//将流的第10(索引号10)个位置指定为流的开始;
fs.Seek(10, SeekOrigin.Begin);
int i=fs.ReadByte();//读取一个字节
MessageBox.Show(i.ToString());
fs.Close();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog op = new OpenFileDialog();
if (op.ShowDialog()==DialogResult.OK)
{
textBox1.Text = op.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog sf = new SaveFileDialog();
if (sf.ShowDialog()==DialogResult.OK)
{
textBox2.Text = sf.FileName;
}
}
private void btnStart_Click(object sender, EventArgs e)
{
//创建读取流
FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read, FileShare.Read);
FileInfo fi = new FileInfo(textBox1.Text);
byte[] buffer=new byte[1024];
long count= 0;
//创建写入流
FileStream fsm = new FileStream(textBox2.Text, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
while (true)
{
//读取数组
int len= fs.Read(buffer, 0, buffer.Length);
//每次以读取的长度写入
fsm.Write(buffer, 0, len);
count += len;
long p = (count * 100) / fi.Length;
progressBar1.Value = (int)p;
//如果没有读取到内容,跳出循环
if (len==0)
{
break;
}
}
fs.Close();
fsm.Close();
}
private void timer1_Tick(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
//读取,跳到多少个字节后读取
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog()==DialogResult.OK)
{
string fileName = ofd.FileName;
//读取fileName对应的文件10个字节后的内容
FileStream fs = new FileStream(fileName, FileMode.Open);
//将流的第10(索引号10)个位置指定为流的开始;
fs.Seek(10, SeekOrigin.Begin);
int i=fs.ReadByte();//读取一个字节
MessageBox.Show(i.ToString());
fs.Close();
}
}
}
}
设计界面:
- C#文件流实现文件复制
- IO 流实现文件复制
- 输入输出流-实现复制文件
- 字节流实现文件复制
- C#递归复制文件
- C#文件复制
- 复制文件进度条 C#
- C#文件(夹)复制
- C#复制文件
- c#复制文件
- C# 复制,删除文件
- C#文件复制
- c#复制文件
- C# 复制文件
- 文件复制实现
- Java实现文件复制
- Java实现文件复制
- 实现文件复制命令
- 字符串转换成整数 (bug版)
- 服务器集群架构的设计与选择
- 谷歌2013校园招聘笔试题心得(附答案)
- Linux ixgbe 10G intel 网卡数据包处理流程
- java异常(Exception)
- C#文件流实现文件复制
- Java中static、final用法小结
- asp.net Session丢失问题
- Qt C++ builder模式
- 安卓、OPhone平台蓝牙编程基础
- 整理 : 计算机维护资料杂记
- HDFS Federation
- OpenStack对象存储管理手册(1)关于OpenStack
- STL中erase()小心使用