gpt4 book ai didi

powershell - 使用.NET ZipFile.CreateFromDirectory时,Powershell脚本将用户的主目录用作当前工作目录。怎么修?

转载 作者:行者123 更新时间:2023-12-02 23:26:15 25 4
gpt4 key购买 nike

这是我的脚本(createdistro.ps1)

Add-Type -assembly "system.io.compression.filesystem"

$source = "bin\Debug\"
$destination = "gwtester_signed.zip"

If(Test-path $destination) {Remove-item $destination}
[io.compression.zipfile]::CreateFromDirectory($source, $destination)

请注意我从运行脚本得到的错误消息:
PS C:\dev\DEV7\Test\gwtester> .\createdistro.ps1
Exception calling "CreateFromDirectory" with "2" argument(s): "The file 'C:\Users\bbren\gwtester_signed.zip' already
exists."
At C:\dev\DEV7\Test\gwtester\createdistro.ps1:7 char:1
+ [io.compression.zipfile]::CreateFromDirectory($source, $destination)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : IOException

如您所见,脚本尝试写入 C:\Users\bbren\gwtester_signed.zip,但是当前的工作目录是 C:\dev\etc\

documentation for ZipFile.CreateFromDirectory指出“相对路径被解释为相对于当前工作目录的相对路径”。

怎么了?

最佳答案

我不建议$ PSScriptRoot。

试试这个:
$destination = Join-Path (Get-Location) 'gwtester_signed.zip'
但是根据您的psversion,Expand-Archive怎么样?

关于powershell - 使用.NET ZipFile.CreateFromDirectory时,Powershell脚本将用户的主目录用作当前工作目录。怎么修?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35891439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com