PowerShell中System.IO.FileStream的路径与文件名定义疑问
你的理解有偏差,$Attachmentpath其实是完整的本地文件路径,而非单纯的文件名,具体拆解如下:
$AttachmentFolder:这才是控制文件保存位置的核心变量,它定义了文件要存放的本地文件夹路径(比如C:\InvoiceAttachments\)$AttachmentFileName:是按规则拼接出来的文件名(比如Vendor_AP_20240501_Invoice.pdf)$Attachmentpath = $AttachmentFolder + $AttachmentFileName:将文件夹路径和文件名拼接后,就得到了完整的「文件夹+文件名」路径,比如C:\InvoiceAttachments\Vendor_AP_20240501_Invoice.pdf
对应代码逻辑来看:
$file = $spSourceWeb.GetFile($ItemAttachmentURLPrefix + $attachment) $bytes = $file.OpenBinary() $AttachmentFileName = $FilePrefix + "_AP_" + $InvoiceIDFormatted + "_" + $attachment $Attachmentpath = $AttachmentFolder + $AttachmentFileName # 生成完整本地文件路径 $fs = new-object System.IO.FileStream($Attachmentpath, "OpenOrCreate") # 基于完整路径操作文件 $fs.Write($bytes, 0 , $bytes.Length) $fs.Close()
简单总结:$AttachmentFolder指定了文件要存到哪个本地文件夹,和文件名拼接后形成的$Attachmentpath就是完整的保存路径,FileStream就是基于这个完整路径来创建并写入文件的。
内容的提问来源于stack exchange,提问作者John John
相关产品推荐
相关产品推荐

