将文件读入行分隔数组 [英] Read file into line delimited array

查看:55
本文介绍了将文件读入行分隔数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 32 位版本的 PowerShell,并通过 PowerGUI 脚本编辑器运行它.

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

网上的一切似乎都认为Get-Content以数组形式返回文件;然而,

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

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

输出

System.String

我知道如何使用内置的 .Net 类型来做到这一点;有没有 PoweShell v2.0 的方法来做到这一点?

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

推荐答案

您确定 $xmlFilename 文件有不止一行以 `n 或 `r`n 分隔吗?

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

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

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

这篇关于将文件读入行分隔数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆