Jerkle Berry
Jerkle Berry

Reputation: 35

PowerShell [io.file]::WriteAllBytes() - writing then using a file produces the error "used by another process"

In PowerShell 7.3.4, I'm making a web request, writing the response content to a file, then forwarding the file via another web request.

$response1 = Invoke-WebRequest @getParam
[io.file]::WriteAllBytes(($local + $filename), $response1.content)
$postParam.Form = @{
   'file'   = ($local + $filename)
}
$response2 = Invoke-WebRequest @postParam

Sporadically, I'm getting:

Exception calling "WriteAllBytes" with "2" argument(s): "The process cannot access the file '\\server\folder\File0002.dat' because it is being used by another process."

Is the [io.file]::WriteAllBytes keeping the file open for some reason? From what I read, it should be closed instantly to allow the next step. I'm not using the Create() method, so I don't think I need to worry about streams. Is there a better file writer that's more efficient?

This doesn't happen all the time, maybe 1 time in 20.

Upvotes: 0

Views: 788

Answers (0)

Related Questions