Powershell发布文件

Powershell发布文件,powershell,powershell-4.0,Powershell,Powershell 4.0,我知道Powershell在脚本退出之前不会发布文件。在我的脚本中,我正在写入一个新文件,如下所示: [System.IO.File]::WriteAllText("E:\Outline\newLocations.xml", $locationsContent) 如何释放此文件以便稍后在脚本中读取?writealText方法不会锁定该文件。下面是一个创建新文本文件的示例,使用该方法写入,然后从中读取,然后删除。您也可以通过创建一个文件、写入该文件,然后PowerShell仍然打开并尝试从Win

我知道Powershell在脚本退出之前不会发布文件。在我的脚本中,我正在写入一个新文件,如下所示:

[System.IO.File]::WriteAllText("E:\Outline\newLocations.xml", $locationsContent)

如何释放此文件以便稍后在脚本中读取?

writealText方法不会锁定该文件。下面是一个创建新文本文件的示例,使用该方法写入,然后从中读取,然后删除。您也可以通过创建一个文件、写入该文件,然后PowerShell仍然打开并尝试从Windows资源管理器中删除它来测试这一点。如果需要释放实例,类通常会有一个
Dispose
方法

New-Item "C:\temp\newLocations.txt" -ItemType File

Directory: C:\temp


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        1/11/2017  12:16 PM              0 newLocations.txt

[System.IO.File]::WriteAllText("C:\temp\newLocations.txt", "test")

Get-Content "C:\temp\newLocations.txt"
test

Remove-Item "C:\temp\newLocations.txt"

get-item "C:\temp\newLocations.txt"
get-item : Cannot find path 'C:\temp\newLocations.txt' because it does not exist.