这些PowerShell命令出了什么问题。它不会写入文件"test.txt“
PS> $stuff = @("AAA`n", "BBB`n")
PS> [System.IO.File]::WriteAllLines(".\test.txt", $stuff)
PS> dir
# Nothing here....我的计算机上是否有任何错误配置可能导致此问题?我需要以某种方式重新安装.net吗?
发布于 2021-10-22 19:37:25
尝试dir ~\test.txt -或者使用绝对路径c:\path\to\my\test.txt。更好的做法是查看Set-Content cmdlet。
仅供参考:您的计算机没有任何问题-您使用的是dotnet库,因此您需要更具体(基本上)。Set-Content是实现这一点的powershell方式( .\test.txt的工作方式与您的预期不谋而合)。
发布于 2021-10-22 19:49:57
.Net与您的PowerShell使用的路径不同。解决方案是将相对路径解析为完整路径:
PS C:\> $stuff = @("AA", "BB")
PS C:\> $out_file = ".\out.txt"
PS C:\> New-Item -Force -Path $out_file
PS C:\> $out_full = $(Resolve-Path -Path $out_file)
PS C:\>[System.IO.File]::WriteAllLines(`
".\testing.txt", $out_full)一种更好的方法:
function open_w {
param([string]$path)
write-host "path: $path"
$pathfix = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($path)
return [System.IO.StreamWriter]::new($pathfix)
}https://stackoverflow.com/questions/69682081
复制相似问题