c# - read text file chunks by chunks by scrollbar -
hi have read question :
reading large text files, should incorporating async?
i diged net stack overflow !
the results 14 method none of them not complete !
in 2 last days , working on , tested , benchmarked 14 methods.
for example :
private void method() { filestream fs = new filestream(path, filemode.open, fileaccess.readwrite); int fsbytes = (int) fs.length; int chunksize = 24; byte[] b = new byte[chunksize]; int pos; (pos = 0; pos < (fsbytes - chunksize); pos += chunksize) { fs.read(b,0 , chunksize); string content = system.text.encoding.default.getstring(b); richtextbox1.text=content=; } b = new byte[fsbytes - pos]; fs.read(b,0, fsbytes - pos); string content2 = system.text.encoding.default.getstring(b); richtextbox1text=content2; fs.close(); fs.dispose(); }
for 5mb text file , takes long , should ?
this working example of reading text file per stream accomplish trying do. have tested 100 mb text file, , worked well, have see if larger files work well.
this example. bring richtextbox form , vscrollbar. use file 'test.txt' on hard drive 'c:'.
public partial class form1 : form { const int page_size = 64; // in characters int position = 0; // position in stream public form1() { initializecomponent(); } private void vscrollbar1_scroll(object sender, scrolleventargs e) { position = e.newvalue * page_size; readfile(position); } private void readfile(int position) { using (streamreader sr = new streamreader(@"c:\test.txt")) { char[] chars = new char[page_size]; sr.basestream.seek(position, seekorigin.begin); sr.read(chars, 0, page_size); string text = new string(chars); richtextbox1.text = text; } } private void form1_load(object sender, eventargs e) { readfile(position); } }
Comments
Post a Comment