惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

博客园 - 朱煜

项目经理与客户沟通的宜与忌 filestream read方法 循环读取固定文件 c#描述异常处理语句try、catch、finally执行时的相互关系 C#计算文件的MD5值实例 C#获取文件MD5字符串 金旭亮老师的Scoekt编程摘要 Winfrom动态创建控件 多个文件合并成一个大文件后,无法打开,需要从以下几个方面找出问题? 腰围2尺1,2,3,4,5,6,7,8寸分别等于是多少厘米/英寸(对照表) 字符编码中的英文字母、汉字占有的字节长度。 Socket Programming in C#--Conclusion Socket Programming in C#--Server Side Socket Programming in C#--Multiple Sockets Socket Programming in C#--Getting Started Socket Programming in C#--Introduction C#中的is,as关键字 委托/事件与观察者模式 发布符合 .NET Framework 准则的事件(C# 编程指南) 在职研究生硕士学位证书查询和认证方式
转 FileStream Read File
朱煜 · 2014-04-22 · via 博客园 - 朱煜

FileStream Read File [C#]

This example shows how to safely read file using FileStream in C#. To be sure the whole file is correctly read, you should call FileStream.Read method in a loop, even if in the most cases the whole file is read in a single call of FileStream.Read method.

Read file using FileStream

First create FileStream to open a file for reading. Then call FileStream.Read in a loop until the whole file is read. Finally close the stream.
 [C#]

 1 using System.IO;
 2 public static byte[] ReadFile(string filePath)
 3 {
 4   byte[] buffer;
 5   FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
 6   try
 7   {
 8     int length = (int)fileStream.Length;  // get file length
 9     buffer = new byte[length];            // create buffer
10     int count;                            // actual number of bytes read
11     int sum = 0;                          // total number of bytes read
12 
13 
14     // read until Read method returns 0 (end of the stream has been reached)
15     while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
16       sum += count;  // sum is a buffer offset for next reading
17   }
18   finally
19   {
20     fileStream.Close();
21   }
22   return buffer;
23 }