PowerShell:读取在其他程序中打开的文件时的错误
Hello PowerShell专家,
将文件添加到zip文件时,下面的脚本片段可工作。但是,如果要添加的文件是在另一个程序中打开的,则它将失败,而“该过程无法访问文件[..]”。我尝试使用[io.fileshare] :: rearwrite,但还没有成功。
关于如何在另一个程序中打开文件,如何打开如何打开读取和写作的文件对zip进行读取和写作的建议?
# write entries with relative paths as names
foreach ($fname in $FullFilenames) {
$rname = $(Resolve-Path -Path $fname -Relative) -replace '\.\\',''
Write-Output $rname
$zentry = $zip.CreateEntry($rname)
$zentryWriter = New-Object -TypeName System.IO.BinaryWriter $zentry.Open()
$zentryWriter.Write([System.IO.File]::ReadAllBytes($fname)) #FAILS HERE
$zentryWriter.Flush()
$zentryWriter.Close()
}
Hello PowerShell Experts,
The script snippet below works when adding files to Zip file. However, if the file to be added is open in another program then it fails with exception, "The process cannot access the file[..]". I tried using [IO.FileShare]::ReadWrite but no success yet.
Any suggestion as to how to open the files for reading and writing to zip regardless whether the file is open in another program or not?
# write entries with relative paths as names
foreach ($fname in $FullFilenames) {
$rname = $(Resolve-Path -Path $fname -Relative) -replace '\.\\',''
Write-Output $rname
$zentry = $zip.CreateEntry($rname)
$zentryWriter = New-Object -TypeName System.IO.BinaryWriter $zentry.Open()
$zentryWriter.Write([System.IO.File]::ReadAllBytes($fname)) #FAILS HERE
$zentryWriter.Flush()
$zentryWriter.Close()
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于我们错过了您的代码中一些重要的部分,因此我只是假设在这种情况下可能有效的内容,并根据您的评论遵循假设。
首先,您将使用 <<代码> fileshare.readwrite :
然后您应该能够使用 /a>:
Since we're missing some important part of your code, I'll just assume what might work in this case, and following assumptions based on your comments.
First you would open the file with
FileShare.ReadWrite
:Then you should be able to use the
.CopyTo(Stream)
method fromFileStream
: