文件流操作 using (System.IO.FileStream fs = new FileStream(path,FileMode.Open)) { byte trunk = new byte[100]; fs.Write(trunk,0,trunk.Length);}读取前100个byte
public static byte[] ReadBytes(string filePath, int n) { using (var fs = File.OpenRead(filePath)) using (var br = new BinaryReader(fs)) return br.ReadBytes(n); }
/// <summary> /// 获取指定文件的 字节片段 /// </summary> public static byte[] GetFilePackageBytes(string fullPath, long offset, long bufferSize) { if (!File.Exists(fullPath)) return null; if (offset < 0) offset = 0; if (bufferSize < 0) bufferSize = long.MaxValue; const int PACKAGE_SIZE = 1024 * 8; using (FileStream myFs = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { myFs.Position = offset; long filePackageLength = Math.Min(bufferSize, myFs.Length - myFs.Position); byte[] bytes = new byte[filePackageLength]; long readLength = 0; while (readLength < filePackageLength) { long leaveLength = myFs.Length - myFs.Position; long leaveLength2 = filePackageLength - readLength; int byteLength = (leaveLength > (long)PACKAGE_SIZE) ? PACKAGE_SIZE : Convert.ToInt32(leaveLength); byteLength = (leaveLength2 > (long)byteLength) ? byteLength : Convert.ToInt32(leaveLength2); byte[] buffer = new byte[byteLength]; myFs.Read(buffer, 0, byteLength); Array.Copy(buffer, 0, bytes, readLength, byteLength); readLength = readLength + byteLength; if (myFs.Position >= myFs.Length) break; } myFs.Close(); return bytes; } } 调用: byte[] bytes = GetFilePackageBytes("D:\Test.rmvb",0,10);测试过:4个 G的 电影,想 读取哪一段,就能读取 哪一段。
http://blog.csdn.net/keenweiwei/article/details/17021779
System.IO.File.ReadAllBytes(path);
或者按二进制文件的形式读,长度自己定义
using (System.IO.FileStream fs = new FileStream(path,FileMode.Open))
{
byte trunk = new byte[100];
fs.Write(trunk,0,trunk.Length);}读取前100个byte
public static byte[] ReadBytes(string filePath, int n)
{
using (var fs = File.OpenRead(filePath))
using (var br = new BinaryReader(fs))
return br.ReadBytes(n);
}
/// 获取指定文件的 字节片段
/// </summary>
public static byte[] GetFilePackageBytes(string fullPath, long offset, long bufferSize)
{
if (!File.Exists(fullPath)) return null; if (offset < 0) offset = 0;
if (bufferSize < 0) bufferSize = long.MaxValue; const int PACKAGE_SIZE = 1024 * 8; using (FileStream myFs = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
myFs.Position = offset;
long filePackageLength = Math.Min(bufferSize, myFs.Length - myFs.Position);
byte[] bytes = new byte[filePackageLength]; long readLength = 0;
while (readLength < filePackageLength)
{
long leaveLength = myFs.Length - myFs.Position;
long leaveLength2 = filePackageLength - readLength;
int byteLength = (leaveLength > (long)PACKAGE_SIZE) ? PACKAGE_SIZE : Convert.ToInt32(leaveLength);
byteLength = (leaveLength2 > (long)byteLength) ? byteLength : Convert.ToInt32(leaveLength2); byte[] buffer = new byte[byteLength];
myFs.Read(buffer, 0, byteLength); Array.Copy(buffer, 0, bytes, readLength, byteLength);
readLength = readLength + byteLength; if (myFs.Position >= myFs.Length) break;
} myFs.Close();
return bytes;
}
}
调用: byte[] bytes = GetFilePackageBytes("D:\Test.rmvb",0,10);测试过:4个 G的 电影,想 读取哪一段,就能读取 哪一段。
实在不好意思,两位大神的代码能完成任务,应该给两位大神各100分,可是没分了,实在不好意思!.
6楼的代码看起来更细心.就给6楼了,下次再送两个大神!sorry