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#]

 using System.IO;
public static byte[] ReadFile(string filePath)
{
byte[] buffer;
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
try
{
int length = (int)fileStream.Length; // get file length
buffer = new byte[length]; // create buffer
int count; // actual number of bytes read
int sum = ; // total number of bytes read // read until Read method returns 0 (end of the stream has been reached)
while ((count = fileStream.Read(buffer, sum, length - sum)) > )
sum += count; // sum is a buffer offset for next reading
}
finally
{
fileStream.Close();
}
return buffer;
}

转 FileStream Read File的更多相关文章

  1. 【基础巩固】文件流读写、大文件移动 FileStream StreamWriter File Path Directory/ ,m资料管理器(递归)

    C#获取文件名 扩展名 string fullPath = @"d:\test\default.avi"; string filename = Path.GetFileName(f ...

  2. C#读操作(字节/字符)Filestream、File、StreamReader

    方法一:使用Filestream,将文本一次性全部转换为字节,之后转换为string显示在text中 OpenFileDialog fd = new OpenFileDialog(); fd.Filt ...

  3. .net学习之集合、foreach原理、Hashtable、Path类、File类、Directory类、文件流FileStream类、压缩流GZipStream、拷贝大文件、序列化和反序列化

    1.集合(1)ArrayList内部存储数据的是一个object数组,创建这个类的对象的时候,这个对象里的数组的长度为0(2)调用Add方法加元素的时候,如果第一次增加元神,就会将数组的长度变为4往里 ...

  4. File,FileInfo,FileStream,StreamReader的区别与用法

    概括的说,File,FileInfo,FileStream是用于文件 I/O 的类,StreamReader是用于从流读取和写入流的类,使用之前都需using System.IO. 先定义一个TXT文 ...

  5. stream,file,filestream,memorystream简单的整理

    一.Stream 什么是stream?(https://www.cnblogs.com/JimmyZheng/archive/2012/03/17/2402814.html#no8) 定义:提供字节序 ...

  6. 【C# IO 操作】 Path 路径类 |Directory类 |DirectoryInfo 类|DriveInfo类|File类|FileInfo类|FileStream类

    Directory类 Directory类 是一个静态类,常用的地方为创建目录和目录管理. 一下来看看它提供的操作. 1.CreateDirectory 根据指定路径创建目录.有重载,允许一次过创建多 ...

  7. C#File类常用的文件操作方法(创建、移动、删除、复制等)

    File类,是一个静态类,主要是来提供一些函数库用的.静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和 打开一个文件. File类方法的参量很多时候都是路径path ...

  8. Csharp--Read Csv file to DataTable

    在网上找的资料都不怎么好使,许多代码一看就知道根本没有考虑全面. 最后找到一个好用的,在codeproject上,这位老兄写成了一个framework,太重了. http://www.codeproj ...

  9. c#FileStream文件读写(转)

    FileStream对象表示在磁盘或网络路径上指向文件的流.这个类提供了在文件中读写字节的方法,但经常使用StreamReader或StreamWriter执行这些功能.这是因为FileStream类 ...

随机推荐

  1. 认识Activity,创建第一个android应用-Hello Word

    2016-04-05 配置好Java.eclipse和Android环境就花费了一天时间.下载SDK真是费了不少时间.现在终于找到解决SDK更新的好方法了(更新自己电脑上的hosts文件,就可以使用G ...

  2. Asp.Net Web Form 前后台传值

    1,后台往前台传值----单个变量直接传递到页面元素 前台代码 <b><%=strCompanyName%>费用明细</b> 后台代码 public partial ...

  3. Oracle脚本笔记

    //创建一个表create table  表名(字段名1 varchar2(20) not null);//多个用 , 隔开//添加字段alter table 表名add (字段名2 varchar2 ...

  4. ksvcreate: Process(m000) creation failed

    一测试服务器数据库(Oracle Database 10g Release 10.2.0.5.0 - 64bit Production)突然访问不了,检查发现数据库处于挂起模式(hang mode), ...

  5. java统计汉字

    public class TotalUtil { public static int getSum(String text) {        String reg = "^[\u4e00- ...

  6. Linux学习--------二

    Linux基础知识 Linux文件系统为一个倒转的单根树状结构文件系统的根为"/" 文件系统严格区分大小写路径 使用"/"分割(windows使用"\ ...

  7. Fatal error: Call-time pass-by-reference has been removed

    下面的代码报错:Fatal error: Call-time pass-by-reference has been removed function myFunc($arg) { do somethi ...

  8. proteus汉化

    下载地址: http://files.cnblogs.com/files/xiaobo-Linux/proteus7%E6%B1%89%E5%8C%96.zip (别的版本也应该可以汉化) 将这ARE ...

  9. 构建 ARM Linux 4.7.3 嵌入式开发环境 —— BusyBox 构建 RootFS

    上一篇我们已经成功将 ARM Linux 4.7.3 的内核利用 U-BOOT 引导了起来.但是细心的你会发现,引导到后面,系统无法启动,出现内核恐慌 (Kernel Panic). 原因是找不到文件 ...

  10. Openxml入门---Openxm读取Excel数据

    Openxml读取Excel数据: 有些问题,如果当Cell 里面是 日期和浮点型的话,对应的Cell.DataType==Null,对应的时间会转换为一个浮点型,对于这块可以通过DateTime.F ...