powershell - Is there a method to determine if a System.IO.StreamWriter object is open? -


im trying determine if streamwriter object open or closed in script. if it's not closed, can write new line file. if closed, need perform other actions....

how can verify if stream still open?

example test script.

$stream = [system.io.streamwriter] "c:\testing.txt" $stream.writeline("test") $stream.close() if($stream)){     #stream still open, write new line     $stream.writeline("stream still open.  write.")     $stream.close() }else(     #stream not open... end script, send reponse. } 

you can check if basestream exists streamwriter-object. this:

if($stream.basestream)){     #stream open } 

example:

$sw = new-object system.io.streamwriter "c:\test.txt"  ps > $sw | fl *   autoflush      : false basestream     : system.io.filestream encoding       : system.text.utf8encoding formatprovider : nb-no newline        :   ps > if($sw.basestream) { "yes" } yes  ps > $sw.close()  ps > if($sw.basestream) { "yes" }  ps > $sw   autoflush      : false basestream     :  encoding       :  formatprovider : nb-no newline        :  

edit or can check 1 of these:

if($sw.basestream.canwrite) {     #you have permission write = stream open , writeable     }  #or  if($sw.basestream.safefilehandle) {     #you have filehandle = stream open     } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -