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:
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!
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)";
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!
Comment