vb.net - Error with code? -


hi can tell me why following dose not work:

(p.s dont want file append upon clicking abutton upon clicking checkbox.

    private sub checkbox1_checkedchanged(byval sender system.object, byval e system.eventargs) handles checkbox1.checkedchanged     dim file_name string = "c:\rxf\log.txt"      'adding items autocad 2006...     if checkbox1.checkstate = checkstate.checked         dim objwriter new system.io.streamwriter(file_name, true)         objwriter.writeline("module: 4fnv-67-5h")         objwriter.close()     end if end sub 

end class

not reproducible, exact code posted. works fine me, creating text file in specified location if 1 not exist , appending specified text end of file.

the thing suggest wrapping streamwriter object in using statement ensure dispose method gets called, if exception thrown (which more when you're doing disk i/o). so, existing code change to:

private sub checkbox1_checkedchanged(byval sender system.object, byval e system.eventargs) handles checkbox1.checkedchanged     dim file_name string = "c:\rxf\log.txt"      ''#adding items autocad 2006...     if checkbox1.checkstate = checkstate.checked         using objwriter new system.io.streamwriter(file_name, true)             objwriter.writeline("module: 4fnv-67-5h")             objwriter.close()         end using     end if end sub 

also, if anticipate method getting called lot (i.e., user clicking , unclicking , clicking checkbox repeatedly), might consider creating streamwriter object once , saving private class-level variable, instead of creating , disposing of each time method called. have make sure dispose of whenever class (presumably containing form) disposed.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -