首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#简略地读取文件

2012-11-08 
C#简单地读取文件用C#来读取文件首先new 一个File类FileStream fs File.OpenRead(filename)或者用一种

C#简单地读取文件

用C#来读取文件

首先new 一个File类

FileStream fs = File.OpenRead(filename);

或者用一种通用的方式来打开:

FileStream fs = FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);

?

接着,需要一个数组:byte?[] data = new byte[fs.Length];

?

然后就可以用fs.Read (data, begain_index, length); 来读取:

fs.Read (data, 0, data.Length);

这个函数有返回值,表示读到的长度。下面我们加利用:

public static void MyReading (byte[] data, Stream stream){int offset=0;// 用一个while循环来反复地读取文件,直到读完while (true){int read = stream.Read(data, offset, 10); //10个字节为一个单位来读if (read <= 0) break;                //在这里面使用data ……offset += read; //指针后移}}

?

热点排行