winforms - Saving files with name read from text box. C# -
i have created code saves work text file, wondering there way when click 'save' can read in text richtextbox1
and set default file name, still 'txt' default file extension.
e.g. when click 'save' folder dialog comes , asks name file, if using word example, want box have text richtextbox1 in.
thanks.
private void savetoolstripmenuitem_click(object sender, eventargs e) { savefiledialog save = new savefiledialog(); save.initialdirectory = "c:\\to-do-list"; save.filter = "text files (*.txt)|*.txt"; save.defaultext = ".txt"; dialogresult result = save.showdialog(); if (result == dialogresult.ok) { using (streamwriter sw = new streamwriter(save.filename)) { sw.writeline(richtextbox1.text); sw.writeline(richtextbox2.text); sw.writeline(richtextbox3.text); sw.writeline(richtextbox4.text); sw.writeline(richtextbox5.text); sw.close(); } }
just set filename
property on savefiledialog
. add
save.filename = string.format("{0}.txt", richtextbox1.text);
before call showdialog
.
Comments
Post a Comment