streamreader outofmemory
我在调用streamreader.readtoend()读一个有四百多K的文件时,程序报System.OutOfMemoryException异常,请问各位大侠怎样处理?
------解决方案--------------------
写一个大文件读取类
public class FileSplitSteamReader : FileStream
{
#region //文件分块读
/// <summary>
/// 用于普通文件读取
/// </summary>
/// <param name="sourceFileName">文件的路径</param>
public FileSplitSteamReader(string sourceFileName)
: base(sourceFileName, FileMode.Open, FileAccess.Read)
{
this.sourceFileName = sourceFileName;
}
/// <summary>
/// 用于大文件读取
/// </summary>
/// <param name="sourceFileName">文件的路径</param>
/// <param name="splitSize">切分大小</param>
public FileSplitSteamReader(string sourceFileName, int splitSize)
: base(sourceFileName, FileMode.Open, FileAccess.Read)
{
this.sourceFileName = sourceFileName;
this.splitSize = splitSize;
}
private string sourceFileName;
/// <summary>
/// 获取文件的路径
/// </summary>
public string SourceFileName
{
get { return sourceFileName; }
}
private long splitSize;
/// <summary>
/// 每次切分文件大小splitSize
/// </summary>
public long SplitSize
{
get { return splitSize; }
}
/// <summary>
/// 文件的大小
/// </summary>
public long FileSize
{
get { return this.Length; }
}
private long readTimes = 1;
/// <summary>
/// 当前读取次数块号
/// </summary>
public long ReadTimes
{
get { return readTimes; }
}
private bool judge = false;
/// <summary>
/// 用于判断是否执行到最后一块.读完为ture,未读完为false.
/// </summary>
public bool Judge
{
get { return judge; }
set { judge = value; }
}
/// <summary>
/// 最后一次读取文件的大小
/// </summary>
public long FinilReadSize
{
get
{
if (splitSize == 0)
return 1024 * 1024 * 3;
else
return this.FileSize - (this.FileSize / (long)this.splitSize) * (long)this.splitSize;
}
}
public int CurrentReadSize;
/// <summary>
/// 开始读取文件
/// </summary>
/// <returns>以Bitmap类型返回每次读取文件的内容</returns>
public byte[] SpliteRead()
{
FileBlockSteamReaderEventArgs Fbsr = new FileBlockSteamReaderEventArgs();
byte[] timeReadContect;
this.Seek(splitSize * (readTimes - 1), SeekOrigin.Begin);
if (readTimes < (this.FileSize / this.splitSize + 1))
{
Fbsr.ReadPercent = (int)(((float)this.Position / this.FileSize) * 100);
timeReadContect = new byte[this.splitSize];
}
else
{
timeReadContect = new byte[this.FinilReadSize];
judge = true;
Fbsr.ReadPercent = 100;
}
CurrentReadSize = timeReadContect.Length;
this.Read(timeReadContect, 0, timeReadContect.Length);
FileABlockReadEndEvent(Fbsr);
readTimes++;<