c# - Is there a way to check if a TextWriter is closed? -
i'm writing class exports data csv file, , constructor takes in textwriter. reason i'm using textwriter rather streamwriter make testing easier: can use same constructor writing streamwriter (which writes files, intended use-case) , write stringwriter (which useful testing).
in constructor perform validation on passed in textwriter. problem can't seem figure out how check if textwriter open or closed. it's possible streamwriter if basestream property null. textwriter not have property however. there way of checking if textwriter open or not?
you may try this:
if( writer.basestream != null) { writer.writeline("writer open"); } else { messagebox.show ("writer closed"); }
i.e, if basestream
null, writer disposed.
also recommended use using
block takes care of this.
Comments
Post a Comment