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
Post a Comment