---
title: "How to write super fast file streaming code in C#?"  
description: "How to write super fast file streaming code in C#?"  
author: "Anonymous User"  
published: 2014-02-09  
updated: 2014-02-09  
canonical: https://www.mindstick.com/forum/1960/how-to-write-super-fast-file-streaming-code-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# How to write super fast file streaming code in C#?

I have to split huge file into many [smaller files](https://www.mindstick.com/interview/34128/how-do-you-split-a-large-file-into-multiple-smaller-files-based-on-size). each of the [destination](https://yourviews.mindstick.com/story/1451/gorgeous-locations-for-destination-weddings-in-india) file is defined by [offset](https://www.mindstick.com/forum/159824/explain-the-purpose-of-the-limit-and-offset-clauses-and-how-are-they-used-for-pagination) and [length](https://www.mindstick.com/interview/1915/what-is-the-difference-between-char_length-and-length) as number of bytes. I'm using the following code:

[private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) void copy([string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) srcFile, string dstFile, int offset, int length)

{

BinaryReader reader = new BinaryReader(File.OpenRead(srcFile));

reader.BaseStream.Seek(offset, SeekOrigin.Begin);

byte[] [buffer](https://www.mindstick.com/forum/160018/explain-the-use-of-the-buffer-class-in-node-js) = reader.ReadBytes(length);

BinaryWriter writer = new BinaryWriter([File.OpenWrite](https://www.mindstick.com/interview/34098/what-is-the-difference-between-file-open-file-openread-and-file-openwrite)(dstFile));

writer.Write(buffer);

}

Considering that I have to call this [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) about 100,000 times, it is remarkably slow.

Is there a way to make the Writer [connected](https://www.mindstick.com/articles/12941/link-building-and-search-engine-rankings-how-are-they-connected) directly to the Reader?

## Replies

### Reply by Pravesh Singh

Hi Ankita,

How large is length? You may do better to re-use a fixed sized (moderately large, but not obscene) buffer, and forget BinaryReader... just use Stream.Read and Stream.Write.

private static void copy(string srcFile, string dstFile, int offset,

int length, byte[] buffer)

{

using(Stream inStream = File.OpenRead(srcFile))

using (Stream outStream = File.OpenWrite(dstFile))

{

inStream.Seek(offset, SeekOrigin.Begin);

int bufferLength = buffer.Length, bytesRead;

while (length > bufferLength &&

(bytesRead = inStream.Read(buffer, 0, bufferLength)) > 0)

{

outStream.Write(buffer, 0, bytesRead);

length -= bytesRead;

}

while (length > 0 &&

(bytesRead = inStream.Read(buffer, 0, length)) > 0)

{

outStream.Write(buffer, 0, bytesRead);

length -= bytesRead;

}

}

}


---

Original Source: https://www.mindstick.com/forum/1960/how-to-write-super-fast-file-streaming-code-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
