Get-content和[IO.File] :: ReadAllText有什么区别?

时间:2019-03-20 05:23:58

标签: powershell file-io locking

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

1 个答案:

答案 0 :(得分:0)

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

# 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查看在代码中使用它们时所采取的步骤。