Fast way of reading in files with C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Riebart
    New Member
    • Nov 2007
    • 1

    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:

    Code:
    			System.IO.FileStream fr = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
    			System.IO.BinaryReader br =  new System.IO.BinaryReader(fr);
    			byte[] bin = br.ReadBytes(Convert.ToInt32(fr.Length));
    			fr.Close();
    			br.Close();
     
    			menuFileNew_Click(null, null);
    			Form activeChildForm = this.ActiveMdiChild;
    			TextBox activeTextBox = (TextBox)activeChildForm.ActiveControl;
    
    			string temp = "";
    			if (activeTextBox != null)
    				for (int i = 0 ; i <= bin.GetUpperBound(0) ; i++)
    					temp += (char)bin[i];
    
    			activeTextBox.Text = temp;
    
    			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!
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Riebart
    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:

    Code:
    			System.IO.FileStream fr = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
    			System.IO.BinaryReader br =  new System.IO.BinaryReader(fr);
    			byte[] bin = br.ReadBytes(Convert.ToInt32(fr.Length));
    			fr.Close();
    			br.Close();
     
    			menuFileNew_Click(null, null);
    			Form activeChildForm = this.ActiveMdiChild;
    			TextBox activeTextBox = (TextBox)activeChildForm.ActiveControl;
    
    			string temp = "";
    			if (activeTextBox != null)
    				for (int i = 0 ; i <= bin.GetUpperBound(0) ; i++)
    					temp += (char)bin[i];
    
    			activeTextBox.Text = temp;
    
    			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.

    Comment

    Working...