winforms - Open/save multiple textbox values c# windows forms -
i created simple example , can't find solution anywhere. want save ui textbox data, , open in another(same) windows form file. here code example:
private void btnsave_click(object sender, eventargs e) { savefiledialog sfd = new savefiledialog(); if (sfd.showdialog() == dialogresult.ok) { streamwriter write = new streamwriter(file.create(sfd.filename)); write.write(txtfirstinput.text); write.write(txtsecondinput.text); write.close(); write.dispose(); } } private void btnopen_click(object sender, eventargs e) { openfiledialog ofd = new openfiledialog(); if(ofd.showdialog() == dialogresult.ok) { streamreader read = new streamreader(file.openread(ofd.filename)); txtfirstinput.text = read.readline(); txtsecondinput.text = read.readline(); read.close(); read.dispose(); } }
the problem here inputed data in first textbox. how separate it? easiest , efficient way? should create , use byte buffer (and hxd) streaming through user input?
can please give me directions or examples.
thank time
you use readline read data back, need use writeline when writing data disk
private void btnsave_click(object sender, eventargs e) { savefiledialog sfd = new savefiledialog(); if (sfd.showdialog() == dialogresult.ok) { using(streamwriter write = new streamwriter(file.create(sfd.filename))) { write.writeline(txtfirstinput.text); write.writeline(txtsecondinput.text); } } }
using write, data written disk without line carriage return , on 1 single line. when call readline data read , put on first textbox.
also notice recommended enclose idisposable object streamwriter inside using statement. approach ensure stream correctly closed , disposed in case of exceptions. (the same should applied streamreader below)
Comments
Post a Comment