vb.net - Error saying file is already open when writing to a file -
i have form fields saving text file.
on same form have button allows view saved prior text file
when hit button reads textfile , displays on datagrid.
this code displaying datagrid
dim reader streamreader reader = new streamreader("c:\users\john\desktop\tickets.txt") dim string dim results() string = reader.readline results = a.split(vbtab) dgv.rows.add(results(0), results(1), results(2), results(3), results(4), results(5)) ' code here ' loop until nothing reader.close() reader.dispose()
this code saves file
dim file streamwriter file = new streamwriter("c:\users\john\desktop\tickets.txt", true) file.writeline(txtname.text & vbtab & dtdate.text & vbtab & txttime.text & vbtab & txtwho.text & vbtab & txtproblem.text & vbtab & txtresolution.text) file.close()
when read records first try save them keep getting error process cannot access file because being used process.
i closed both reader , writer, , not sure why happeneing
better code, sure loop ending?
write:
dim path string = "c:\users\john\desktop\tickets.txt" if file.exists(path) using sw new streamwriter(path, true) 'code end using' closes , disposes end if
read:
dim path string = "c:\users\john\desktop\tickets.txt" if file.exists(path) dim results new list(of string) using sr new streamreader(path) while not sr.endofstream() results.add(sr.readline()) 'better loop end while end using' closes , disposes end if each line in results 'add dgv next
Comments
Post a Comment