c# - Invalid Operation Exception in file saving -
i have code , don't know why pops error message :
"invalid operation exception unhandled user code".
this error comes out when press save button.
the purpose of program save text 1 textbox in mytest.txt file , file textbox1. appreciate here. thank in advance.
public mainpage() { this.initializecomponent(); } private void buttonsave_click(object sender, routedeventargs e) { string path = @"c:\users\geora\mytest.txt"; if(!file.exists(path)) { using (streamwriter sw = file.createtext(path)) { sw.writeline(textbox.text); } } } private void buttonshow_click(object sender, routedeventargs e) { string path = @"c:\users\geora\mytest.txt"; using (streamreader sr = file.opentext(path)) { string s = ""; s = sr.readline(); textbox1.text = s; } }
you have opened files not closed file.this might issue
streamreader sr = file.opentext(path)
you need close it. using (and disposes it's gc'd sooner):
or alternatively in .net 2 can use new file. static members, don't need close anything:
variable = file.readalltext(path);
Comments
Post a Comment