c# - Write text from .txt file to Rich Text Box from different text files? -


so i'm making program needs write info text box txt file read info txt file in rich text box. heres have far.

private void btn_write_click(object sender, eventargs e) {     if (file.exists(@"c:\windows\temp\" + txt_key.text + ".txt"))         messagebox.show("file exists");         system.diagnostics.process.start(@"c:\windows\temp\" + txt_key.text + ".txt");     else     {         streamwriter sw = new streamwriter(@"c:\windows\temp\" + txt_key.text + ".txt", true);          sw.writeline("dog: " + txt_name.text);          txt_name.clear();         sw.writeline("owner: " + txt_owner.text);          txt_owner.clear();         sw.writeline("age: " + nud_age.value.tostring());         nud_age.value = 0;          sw.writeline("breed: " + cmb_breed.selecteditem.tostring());          if (cmb_breed.selectedindex == 0)         {             sw.writeline("sub specie: " + cmb_shepard.selecteditem.tostring());             cmb_shepard.selectedindex = -1;         }         else if (cmb_breed.selectedindex == 7 && ckb_pedigree.checked == true)         {             sw.writeline("pedigree: yes");         }         else if (cmb_breed.selectedindex == 7 && ckb_pedigree.checked == false)         {             sw.writeline("pedigree: no");         }         txt_key.clear();         sw.writeline("comments: " + txt_com.text);         sw.close();         txt_com.clear();     } }  private void btn_read_click(object sender, eventargs e) {     streamreader sr = new streamreader(@"c:\windows\temp\" + txt_key.text, true);     rtb_info.text = sr.readtoend();     sr.close(); } 

as can see in write button have

streamwriter sw = new streamwriter(@"c:\windows\temp\" + txt_key.text + ".txt", true); 

which work , makes txt file key generated in read button says file doesn't exist. appreciated.

i suspect, because code clearing txt_key text value after writing file.

you should think of either removing below line or having class field , preserving next read.

 txt_key.clear(); 

i suggest use debugger step in , see exact path been constructed.

on side note, not advisable write os folder.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -