当前位置:网站首页>[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
}
边栏推荐
- STM32F4---通用定时器更新中断
- RC振荡器和晶体振荡器简介
- 解密函数计算异步任务能力之「任务的状态及生命周期管理」
- FLIR blackfly s usb3 industrial camera: how to use counters and timers
- [paper reading | deep reading] graphsage:inductive representation learning on large graphs
- [leetcode] day97 remove linked list elements
- 激光雷达:Ouster OS产品介绍及使用方法
- 【论文阅读|深读】 GraphSAGE:Inductive Representation Learning on Large Graphs
- The foreground downloads network pictures without background processing
- SchedulX V1.4.0及SaaS版发布,免费体验降本增效高级功能!
猜你喜欢
Collection recommandée!! Quel plug - in de gestion d'état flutter est le plus fort? Regardez le classement des manons de l'île, s'il vous plaît!
Flir Blackfly S USB3 工业相机:白平衡设置方法
FLIR blackfly s industrial camera: auto exposure configuration and code
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
新一代云原生消息队列(一)
建议收藏!!Flutter状态管理插件哪家强?请看岛上码农的排行榜!
Lumion 11.0 software installation package download and installation tutorial
【论文阅读|深读】DNGR:Deep Neural Networks for Learning Graph Representations
[paper reading | deep reading] rolne: improving the quality of network embedding with structural role proximity
Analyze "C language" [advanced] paid knowledge [i]
随机推荐
GEE升级,可以实现一件run tasks
#夏日挑战赛#数据库学霸笔记(下)~
How do I dump SoapClient requests for debugging- How to dump SoapClient request for debug?
centos8 用yum 安装MySQL 8.0.x
leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】
Web开发小妙招:巧用ThreadLocal规避层层传值
Integrated navigation: product description and interface description of zhonghaida inav2
Blackfly s usb3 industrial camera: buffer processing
建议收藏!!Flutter状态管理插件哪家强?请看岛上码农的排行榜!
Draco - glTF模型压缩利器
Tiflash source code reading (IV) design and implementation analysis of tiflash DDL module
Introduction to FLIR blackfly s industrial camera
Jacob Steinhardt, assistant professor of UC Berkeley, predicts AI benchmark performance: AI has made faster progress in fields such as mathematics than expected, but the progress of robustness benchma
Threadlocalutils (tool class IV)
The GPG keys listed for the "MySQL 8.0 community server" repository are already ins
Redis tool class redisutil (tool class III)
Metaforce force meta universe development and construction - fossage 2.0 system development
Lombok同时使⽤@Data和@Builder 的坑
Lumion 11.0软件安装包下载及安装教程
机器人队伍学习方法,实现8.8倍的人力回报