Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Powershell CreateFromDirectory进程无法访问文件,因为其他进程正在使用该文件_C#_Windows_Powershell_Server_Windows Server 2012 - Fatal编程技术网

C# Powershell CreateFromDirectory进程无法访问文件,因为其他进程正在使用该文件

C# Powershell CreateFromDirectory进程无法访问文件,因为其他进程正在使用该文件,c#,windows,powershell,server,windows-server-2012,C#,Windows,Powershell,Server,Windows Server 2012,我在Windows Server 2012R2上有大量500GB的索引文件,应用程序会不断更新这些文件 我必须使用PowerShell压缩文件,但当我尝试使用以下代码片段压缩文件时,会出现以下异常: 使用“4”参数调用“CreateFromDirectory”时出现异常:“进程无法访问文件'E:\Program Files(x86)\Application Folder\file\Status.FCS',因为它正在使用。” 通过另一个过程。” 如果使用Windows GUI压缩文件,我不会有任何

我在Windows Server 2012R2上有大量500GB的索引文件,应用程序会不断更新这些文件

我必须使用PowerShell压缩文件,但当我尝试使用以下代码片段压缩文件时,会出现以下异常:

使用“4”参数调用“CreateFromDirectory”时出现异常:“进程无法访问文件'E:\Program Files(x86)\Application Folder\file\Status.FCS',因为它正在使用。” 通过另一个过程。”

如果使用Windows GUI压缩文件,我不会有任何问题,但问题似乎只有在使用PowerShell脚本时才会发生。此外,如果我停止应用程序服务,PowerShell脚本也可以正常工作(在prod环境中无法做到这一点)

其中一个解决方案是复制索引为500Gb的文件夹并对其进行压缩(应该可以),但我的Windows服务器上没有足够的磁盘空间来进行压缩


那么,是否有任何解决方案可以在文件被写锁定时使用PowerShell脚本对其进行压缩?

您需要为zip文件设置不同的目录:
[System.IO.directory]::SetCurrentDirectory($inPath)
所以


您需要为zip文件设置不同的目录:
[System.IO.directory]::SetCurrentDirectory($inPath)
所以


恐怕您必须编写自己的目录枚举,将每个文件作为
IO.FileStream
读取,并使用
[IO.FileShare]::ReadWrite
标志,允许打开其他进程使用的文件(除非为显式读取而锁定,但由于GUI正常工作,这里不是这种情况)并写入新的
IO.Compression.ZipArchive
。也许你能找到现有的库/示例。谢谢@wOxxOm。我会研究一下。恐怕您必须编写自己的目录枚举,用
[IO.FileShare]将每个文件作为
IO.FileStream
读取:ReadWrite
标志,该标志允许打开其他进程使用的文件(除非为显式读取而锁定,但由于GUI工作,这里不是这种情况)并写入新的
IO.Compression.ZipArchive
。也许你能找到现有的库/示例。谢谢@wOxxOm。我会调查的。
$zip = "E:\Folder\File.zip"
$can = "E:\Program Files (x86)\Application Folder\File

Import-Module AWSPowerShell

# functions
function ZipFiles( $zipfilename, $sourcedir )
{
    Add-Type -Assembly System.IO.Compression.FileSystem
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,
    $zipfilename, $compressionLevel, $false)
}

ZipFiles $zip $can
Function ZipFiles( $zipFileName, $pathTarget )
{
   Add-Type -Assembly System.IO.Compression.FileSystem
   # Emplacement de sortie
   [System.IO.Directory]::SetCurrentDirectory($inPath)
   $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
   [System.IO.Compression.ZipFile]::CreateFromDirectory($pathTarget, $zipFileName, $compressionLevel, $false)

}