vb.net - CreateText method produces error "The process cannot access the file because it is being used by another process" -
i working on button delete text in gross.txt. keep getting error:
the process cannot access file 'c:\users\isaiah\visual basic programs\vb2012\chap10\gross pay solution\gross pay project\bin\debug\gross.txt' because being used process.
or more specifically:
a first chance exception of type 'system.io.ioexception' occurred in mscorlib.dll.
it appears error comes btndelete_click @ bottom.
option explicit on option strict on option infer off public class frmmain private sub btnexit_click(sender object, e eventargs) handles btnexit.click me.close() end sub private sub txtgrosspay_keypress(sender object, e keypresseventargs) handles txtgrosspay.keypress ' allows text box accept numbers, period, , backspace key if (e.keychar < "0" orelse e.keychar > "9") andalso e.keychar <> "." andalso e.keychar <> controlchars.back e.handled = true end if end sub private sub btnsave_click(sender object, e eventargs) handles btnsave.click dim outfile io.streamwriter outfile = io.file.appendtext("gross.txt") outfile.writeline(txtgrosspay.text) outfile.close() txtgrosspay.text = "" end sub private sub btndisplay_click(sender object, e eventargs) handles btndisplay.click ' declare new infile varable streamreader object dim infile io.streamreader dim dblline double ' opens gross.txt input infile = io.file.opentext("gross.txt") ' .exists() searches /bin folder gross.txt, returns boolean value if io.file.exists("gross.txt") infile = io.file.opentext("gross.txt") 'fill list values until infile.peek = -1 double.tryparse(infile.readline, dblline) lstcontents.items.add(dblline.tostring("c2").padleft(6, " "c)) loop else messagebox.show("the file have requested not exist", "gross pay project", messageboxbuttons.okcancel, messageboxicon.error) end if infile.close() end sub private sub btndelete_click(sender object, e eventargs) handles btndelete.click dim outdelete io.streamwriter outdelete = io.file.createtext("gross.txt") outdelete.close() end sub end class
ok, found issue.
opening file twice in code line
infile = io.file.opentext("gross.txt") i commented out first time did before .exists call. added dispose delete call. createtext doesn't need assign commented out code , added line needed. tested , these changes fixed issue. in fact first change fix, second 1 clean things up.
private sub btndisplay_click(sender object, e eventargs) handles btndisplay.click 'declare new infile varable streamreader object dim infile io.streamreader dim dblline double 'opens gross.txt input ' infile = io.file.opentext("gross.txt") '.exists() searches /bin folder gross.txt, returns boolean value if io.file.exists("gross.txt") infile = io.file.opentext("gross.txt") 'fill list values until infile.peek = -1 double.tryparse(infile.readline, dblline) lstcontents.items.add(dblline.tostring("c2").padleft(6, " "c)) loop infile.close() else messagebox.show("the file have requested not exist", "gross pay project", messageboxbuttons.okcancel, messageboxicon.error) end if end sub private sub btndelete_click(sender object, e eventargs) handles btndelete.click 'dim outdelete io.streamwriter 'outdelete = io.file.createtext("gross.txt") 'outdelete.close() io.file.createtext("gross.txt").dispose end sub
Comments
Post a Comment