当前位置:网站首页>[C # notes] use file stream to copy files
[C # notes] use file stream to copy files
2022-07-07 02:20:00 【Start()】
Namespace to be introduced : System.IO
C# A sealed copy of the file API:
System.IO.File.Copy(); // A copy of a document
System.IO.File.ReadAllBytes(); // Read all byte groups of the file
System.IO.File.WriteAllBytes(); // Write byte group to file
Next, I will do it myself , Use the way of reading and writing the file stream to copy the file , There are three main steps :
public string filePath = @"C:\Users\Njiyue\Desktop\ddd.jpg";
public string copyToPath = @"C:\Users\Njiyue\Desktop\ New folder \ccc.jpg";
private void Test()
{
#region First step : obtain Two files “ flow ”
FileInfo oneFileInfo = new FileInfo(filePath);
if (!File.Exists(copyToPath))
File.Create(copyToPath);// If there is no target file , Just create one
FileInfo twoFileInfo = new FileInfo(copyToPath);
FileStream fs1 = oneFileInfo.OpenRead();// Read the file , Convert file to byte file
FileStream fs2 = twoFileInfo.OpenWrite();// write file , Convert file to byte file
#endregion
#region The second step : Cyclic read write
byte[] buff = new byte[2048];// Read and write once 2048 Bytes
int contentLen = fs1.Read(buff, 0, 2048);// Read the file 2048 Bytes , And cache it to buff Byte group , Returns the read length , If the length is less than 2048, The description reads to the end , If it is equal to 0, It means that there are no bytes to read
// Loop read operation
while (contentLen != 0)
{
fs2.Write(buff, 0, contentLen); // Key method , Write the stream to
contentLen = fs1.Read(buff, 0, 2048);// Call once , Will be read in turn next 2048 byte
}
#endregion
#region The third step : Close the read-write process
// Turn off reading and writing , To avoid occupying files
fs1.Close();
fs2.Close();
fs1 = fs2 = null;
#endregion
}
边栏推荐
- ZABBIX 5.0: automatically monitor Alibaba cloud RDS through LLD
- Alibaba cloud middleware open source past
- 2022/0524/bookstrap
- 【Unity】升级版·Excel数据解析,自动创建对应C#类,自动创建ScriptableObject生成类,自动序列化Asset文件
- ROS learning (24) plugin
- Metaforce force meta universe development and construction - fossage 2.0 system development
- Lumion 11.0 software installation package download and installation tutorial
- 【论文阅读|深读】ANRL: Attributed Network Representation Learning via Deep Neural Networks
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
- PostgreSQL图形化界面工具之pgAdmin4
猜你喜欢

Twenty or thirty thousand a leaf? "Yang Mou" behind the explosion of plant consumption

MySQL execution process and sequence

Batch delete data in SQL - set in entity

豆瓣平均 9.x,分布式领域的 5 本神书!

centos8安裝mysql報錯:The GPG keys listed for the “MySQL 8.0 Community Server“ repository are already ins

激光雷达:Ouster OS产品介绍及使用方法

FLIR blackfly s industrial camera: configure multiple cameras for synchronous shooting

How did partydao turn a tweet into a $200million product Dao in one year

TiFlash 源码阅读(四)TiFlash DDL 模块设计及实现分析

一片葉子兩三萬?植物消費爆火背後的“陽謀”
随机推荐
处理streamlit库上传的图片文件
低代码平台中的数据连接方式(上)
leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】
#yyds干货盘点# 解决名企真题:最大差值
ROS learning (XX) robot slam function package -- installation and testing of rgbdslam
FLIR blackfly s industrial camera: explanation and configuration of color correction and code setting method
The last line of defense of cloud primary mixing department: node waterline design
GEE升级,可以实现一件run tasks
[paper reading | deep reading] rolne: improving the quality of network embedding with structural role proximity
2022/0524/bookstrap
Sensor: introduction of soil moisture sensor (xh-m214) and STM32 drive code
投资的再思考
强化学习如何用于医学影像?埃默里大学最新《强化学习医学影像分析》综述,阐述最新RL医学影像分析概念、应用、挑战与未来方向
一片叶子两三万?植物消费爆火背后的“阳谋”
FLIR blackfly s usb3 industrial camera: white balance setting method
freeswitch拨打分机号源代码跟踪
Flir Blackfly S USB3 工业相机:白平衡设置方法
Lidar: introduction and usage of ouster OS
大咖云集|NextArch基金会云开发Meetup来啦!
SchedulX V1.4.0及SaaS版发布,免费体验降本增效高级功能!