繁体   English   中英

如何提高 Get-Content $picture 转换为字节数组的性能?

[英]How do I improve performance of Get-Content $picture conversion into a byte array?

使用 Powershell,我通过 API 将图片插入 SQL 数据库 varbinary(max) 列。

我使用 Get-Content -Encoding byte 将图片转换为字节数组,但速度很慢:

[byte[]]$picture = Get-Content $picturePath -Encoding byte

有没有办法提高性能?

我最终得到了这个(更快)的解决方案:

[system.io.stream]$Stream = [system.io.File]::OpenRead($picturePath)
try {
  [byte[]]$picture = New-Object byte[] $Stream.length
  [void] $Stream.Read($picture, 0, $Stream.Length);
} finally {
  $Stream.Close();
}

根据@zett42 的提示,我测试了“Get-Content”、“Get-Content with the -Raw 开关”和“system.io.stream”解决方案( https://github.com/mmunchandersen/byte-array-表现)。

结果让我改变了之前的决定,转而使用带有 -Raw 开关的 Get-Content。

在此处输入图像描述

注意 PowerShell 7.2 不支持“编码字节”。 在 7.2 中使用 -AsByteStream 开关( https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content?view=powershell-5.1 / https://docs.microsoft。 com/en-us/powershell/module/microsoft.powershell.management/get-content?view=powershell-7.2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM