i'm doing basic read/write class of examples i've found console input.
i figured out how write file using text box , open needed file.
i'm having trouble writing file on open file path usual save option since other uses savefiledialog
. i'm assuming it's not hard cannot seem fine resources.
i've attached open , save code have.
private void mnuopen_click(object sender, eventargs e) { //promps file dialog open text file openfiledialog dlgopen = new openfiledialog(); dlgopen.filter = "all files|*.*|text files|*.txt*"; dlgopen.multiselect = false; if (dlgopen.showdialog() == dialogresult.ok) { openfile(dlgopen.filename); } } public void openfile(string filepath) { //class reads opened file streamreader reader = null; try { //messagebox.show(filepath) reader = new streamreader(filepath); txtbody.text = reader.readtoend(); stafilepath.text = filepath; stadatetime.text = datetime.now.toshorttimestring(); } catch(exception ex) { messagebox.show(ex.message); } { if (reader != null) reader.close(); } } private void newtoolstripmenuitem_click(object sender, eventargs e) { //new button clicked clears text txtbody.text = string.empty; } private void mnusaveas_click(object sender, eventargs e) { //when save menu button clicked prompt pops , asked save new file savefiledialog dlgsave = new savefiledialog(); dlgsave.filter = "all files|*.*|text files|*.txt*"; if (dlgsave.showdialog() == dialogresult.ok) { savefile(dlgsave.filename); } } public void savefile(string filepath) { //writes on file selected streamwriter writer = null; try { writer = new streamwriter(filepath); writer.write(txtbody.text); stafilepath.text = filepath; stadatetime.text = datetime.now.toshorttimestring(); messagebox.show("file saved!"); } catch(exception ex) { messagebox.show(ex.message); } { if (writer != null) writer.close(); } } private void mnusave_click(object sender, eventargs e) { //saves open file in text box unless not exist file path yet }
Comments
Post a Comment