You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell中System.IO.FileStream的路径与文件名定义疑问

关于SharePoint文件保存路径的解析

你的理解有偏差,$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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 13:50:24