版权声明:知识来源于智慧与善良并存的师傅们,亦当回报给智慧与善良并存的人们。 https://blog.csdn.net/yushaopu/article/details/82312898
慈心积善融学习,技术愿为有情学。善心速造多好事,前人栽树后乘凉。我今于此写经验,愿见文者得启发。
- .NET Framework : 4.6.1
- ide : visual studio 2017 community
- os : win7 x86_64
- type setting : markdown
- blog : https://blog.csdn.net/yushaopu
- github : https://github.com/GratefulHeartCoder
file
code
using System;
using System.IO;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
// 文件的绝对路径
string pathRead = @"E:\Develop\Python\Python37\python37.dll";
// 粘贴并改名字
string pathSave = @"E:\p.dll";
using (FileStream fsRead = new FileStream(pathRead, FileMode.Open, FileAccess.Read))
{
using (FileStream fsWrite = new FileStream(pathSave, FileMode.Create, FileAccess.Write))
{
// 每次读取500KB
byte[] buffer = new byte[500 * 1024];
int r = fsRead.Read(buffer, 0, buffer.Length);
double fileMov = 0;
while (r > 0)
{
fsWrite.Write(buffer, 0, r);
// 存取此次转移的大小是多少KB
fileMov = fileMov + r / 1024.0;
Console.WriteLine("转移了" + r / 1024.0 + "KB");
r = fsRead.Read(buffer, 0, buffer.Length);
}
Console.WriteLine("该文件大小" + fileMov + "KB");
}
}
Console.ReadKey();
}
}
}
result
转移了500KB
转移了500KB
转移了500KB
转移了500KB
转移了500KB
转移了500KB
转移了500KB
转移了254.6484375KB
该文件大小3754.6484375KB
扩展 - 关于Write的介绍
//
// 摘要:
// 将字节块写入文件流。
//
// 参数:
// array:
// 包含要写入该流的数据的缓冲区。
//
// offset:
// array 中的从零开始的字节偏移量,从此处开始将字节复制到该流。
//
// count:
// 最多写入的字节数。
//
// 异常:
// T:System.ArgumentNullException:
// array 为 null。
//
// T:System.ArgumentException:
// offset 和 count 描述 array 中的无效范围。
//
// T:System.ArgumentOutOfRangeException:
// offset 或 count 为负数。
//
// T:System.IO.IOException:
// 出现 I/O 错误。 - 或 - 另一个线程可能导致操作系统的文件句柄的位置发生意外更改。
//
// T:System.ObjectDisposedException:
// 流已关闭。
//
// T:System.NotSupportedException:
// 当前的流实例不支持写入。
参数offset是偏移量,在其他程序中可能非0。有兴趣的同学,可以学习一下ipv6协议。
resource
- [文档] https://docs.microsoft.com/zh-cn/dotnet/csharp/
- [规范] https://github.com/dotnet/docs/tree/master/docs/standard/design-guidelines
- [源码] https://referencesource.microsoft.com/
- [平台] https://www.csdn.net/
- [ IDE ] https://visualstudio.microsoft.com/zh-hans/
感恩曾经帮助过 心少朴 的人。
C#优秀,值得学习。.NET Core具有跨平台的能力,值得关注。
Console,WinForm,WPF,ASP.NET,Azure WebJob,WCF,Unity3d,UWP可以适当地了解。
注:此文是自学笔记所生,质量中下等,故要三思而后行。新手到此,不可照搬,应先研究其理象数,待能变通之时,自然跳出深坑。