forms - C# text file browse and file write -
forms - C# text file browse and file write -
i have big text file (around 35 000+ lines of information) , extract lines , place them in text file.
the text file goes this:
feature info feature name: 123456 version: 1 tokens total: 35 tokens remaining: 10
and i'd extract feature name , tokens total. had in mind form 2 buttons: 1 browsing file , other whole reading , writing file part, of course of study in same line line format.
anyone have clues on how it? have searched , haven't found specific things, quite new file read/write...
edit
ok here have far , works:
private void button1_click(object sender, eventargs e) { int counter = 0; string line; string s1 = "feature name"; string s2 = "tokens total"; // read file , display line line. system.io.streamreader file = new system.io.streamreader("d:\\license.txt"); using (system.io.streamwriter file2 = new system.io.streamwriter(@"d:\test.txt")) while ((line = file.readline()) != null) { if (line.contains(s1) || line.contains(s2)) { file2.writeline(line); counter++; } } file.close();
and done single button. want though able search file want , utilize button in order writing process
answer edit:
you can store read info in property or private field in form class. utilize string or stringbuilder preferably. when sec button clicked check if there stored info , write output file.
private stringbuilder info = new stringbuilder(); private void button2_click(object sender, eventargs e) { if(data.length > 0) { using(system.io.streamwriter file2 = new system.io.streamwriter(@"d:\test.txt")) { file2.write(data.tostring()); } } } private void button1_click(object sender, eventargs e) { // clear previous store info data.clear(); // ... system.io.streamreader file = new system.io.streamreader("d:\\license.txt"); while ((line = file.readline()) != null) { if (line.contains(s1) || line.contains(s2)) { sb.appendline(line); counter++; } } file.close(); }
please add together using system.io , surround streamreader , streamwriter using block code more readable , won't forget release used resources.
c# forms file-io
Comments
Post a Comment