Read file into line delimited array

随声附和 提交于 2019-12-22 18:18:07

问题


I'm using the 32-bit version of PowerShell and running it via the PowerGUI Script Editor.

Everything online seems to think that Get-Content returns the file as an array; however,

$lines = Get-Content -Path $xmlFilename
Write-Host $lines.GetType()

outputs

System.String

I know how to do this using the builtin .Net types; is there a PoweShell v2.0 way of doing this?


回答1:


Are you sure that $xmlFilename file have more than one line separed by `n or `r`n?

if the file contain just a single line you can do it:

 $lines = @(Get-Content -Path $xmlFilename) # this return [object[]] type

or

$lines = ,(Get-Content -Path $xmlFilename)

or

 $lines = [string[]](Get-Content -Path $xmlFilename) # this return [string[]] type


来源:https://stackoverflow.com/questions/17574163/read-file-into-line-delimited-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!