Get-content 和 [IO.File]::ReadAllText 有什么不同?

What is different between Get-content and [IO.File]::ReadAllText?

如果我使用 Get-content 读取另一个进程被占用的文件,那么工作正常。 但是,通过 [IO.File]::ReadAllText 读取文件并显示错误信息: 该文件被另一个进程占用。

这是两者之间的基本概念。

# returns array of lines in the file
Get-Content "FileName.txt"

# returns one string for whole file.
[System.IO.File]::ReadAllText("FileName.txt")

# There are ways to achieve second behavior with Get-Content, as of PowerShellv3 and later

Get-Content "FileName.txt" -Raw

# in PowerShell 2:
Get-Content "FileName.txt" | Out-String

详细信息在 MS 文档中。

File.​Read​All​Text Method

Get-Content (Microsoft.PowerShell.Management)

您可以在 MS PowerShell GitHub 页面查看 Get-Content 的源代码。 如果你真的想看看幕后是什么,或者你可以使用 Trace-Command 来查看你在代码中使用它们时所采取的步骤。