473,395 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Fast way of reading in files with C#

I'm trying to create a Notepad replacement with some extended features (Including encryption and handling some specific file types) and for the most part it is going ok until I try to load a 'large' file. Anything above about 20kb takes FAR too long to load. My open file code goes something like:

Expand|Select|Wrap|Line Numbers
  1.             System.IO.FileStream fr = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
  2.             System.IO.BinaryReader br =  new System.IO.BinaryReader(fr);
  3.             byte[] bin = br.ReadBytes(Convert.ToInt32(fr.Length));
  4.             fr.Close();
  5.             br.Close();
  6.  
  7.             menuFileNew_Click(null, null);
  8.             Form activeChildForm = this.ActiveMdiChild;
  9.             TextBox activeTextBox = (TextBox)activeChildForm.ActiveControl;
  10.  
  11.             string temp = "";
  12.             if (activeTextBox != null)
  13.                 for (int i = 0 ; i <= bin.GetUpperBound(0) ; i++)
  14.                     temp += (char)bin[i];
  15.  
  16.             activeTextBox.Text = temp;
  17.  
  18.             activeChildForm.Text = fileName + " (Plaintext)";
Just through some guesswork, almost all of the time is spent in the for loop and negligible time is spent on the reading of the file and the outputting to the textbox. I suppose it's because of the fact that strings are immutable (Long time since I took any formal computer science so I might have that wrong).

Any ideas on how I could speed this up? My test file is a binary file (It's read as binary as I need to preserve all of it's contents since encryption turns lots of the characters into non-Stream friendly ones) is about 350kb and loads in approximately 1 second in Notepad, but takes so long with this code I haven't even bothered to let it finish.

Any help would be fantabulous!
Nov 9 '07 #1
1 3743
r035198x
13,262 8TB
I'm trying to create a Notepad replacement with some extended features (Including encryption and handling some specific file types) and for the most part it is going ok until I try to load a 'large' file. Anything above about 20kb takes FAR too long to load. My open file code goes something like:

Expand|Select|Wrap|Line Numbers
  1.             System.IO.FileStream fr = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
  2.             System.IO.BinaryReader br =  new System.IO.BinaryReader(fr);
  3.             byte[] bin = br.ReadBytes(Convert.ToInt32(fr.Length));
  4.             fr.Close();
  5.             br.Close();
  6.  
  7.             menuFileNew_Click(null, null);
  8.             Form activeChildForm = this.ActiveMdiChild;
  9.             TextBox activeTextBox = (TextBox)activeChildForm.ActiveControl;
  10.  
  11.             string temp = "";
  12.             if (activeTextBox != null)
  13.                 for (int i = 0 ; i <= bin.GetUpperBound(0) ; i++)
  14.                     temp += (char)bin[i];
  15.  
  16.             activeTextBox.Text = temp;
  17.  
  18.             activeChildForm.Text = fileName + " (Plaintext)";
Just through some guesswork, almost all of the time is spent in the for loop and negligible time is spent on the reading of the file and the outputting to the textbox. I suppose it's because of the fact that strings are immutable (Long time since I took any formal computer science so I might have that wrong).

Any ideas on how I could speed this up? My test file is a binary file (It's read as binary as I need to preserve all of it's contents since encryption turns lots of the characters into non-Stream friendly ones) is about 350kb and loads in approximately 1 second in Notepad, but takes so long with this code I haven't even bothered to let it finish.

Any help would be fantabulous!
Do not use Binary readers for reading character data. Use the TextReader class instead.
Nov 10 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Scott Brady Drummonds | last post by:
Hi, everyone, I'm a relative novice to Python and am trying to reduce the processing time for a very large text file that I am reading into my Python script. I'm currently reading each line one...
0
by: Dean J Garrett | last post by:
Does anyone know about "fast web view" for PDF files? We have a .NET application that opens PDF files as the user's request. The problem is that some of these are very large, 20MB, and it takes...
20
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg.,...
6
by: Cable | last post by:
Hello, I am hoping that someone can answer a question or two regarding file access. I have created an app that reads an image from a file then displays it (using OpenGL). It works well using...
6
by: G.Esmeijer | last post by:
Friends, I would like to read a text file (fixed length formaated) really fast and store the data into an Access database (2003). Using the streamreader and reading line by line, separating the...
4
by: DraguVaso | last post by:
Hi, I have files I need to read, which contains records with a variable lenght. What I need to do is Copy a Part of such a File to a new File, based on the a Begin- and End-record. I used...
4
by: serge | last post by:
I managed to put together C# code and have it do the following: 1- Get all the table names that start with the letter "Z" from sysobjects of my SQL 2000 database and put these table names...
4
by: Alexis Gallagher | last post by:
(I tried to post this yesterday but I think my ISP ate it. Apologies if this is a double-post.) Is it possible to do very fast string processing in python? My bioinformatics application needs to...
9
by: Salad | last post by:
I have access, for testing at my client's site, a Win2000 computer running A2003 retail. He recently upgraded all of his other machines to DualCore Pentiums with 2 gig ram and run A2003 runtime. ...
2
by: secutos | last post by:
I have the option of storing data online as text files, or storing data online in a MySQL database. If the information is stored as a text file, all I have to do is a HttpWebRequest to the text file...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.