2
我没有创建Power Shell脚本的经验,我试图实例化提供特定文本编码的System.IO.StreamWriter类。我试图做到这一点:如何在PowerShell上使用ANSI编码来瞬时化System.IO.StreamWriter?
$file = New-Object System.IO.StreamWriter("sample.txt", [System.Text.Encoding]::Default, 4096)
而且我得到了以下错误消息:
New-Object : Cannot find an overload for "StreamWriter" and the argument count: "3".
At line:1 char:18
+ $out = New-Object <<<< System.IO.StreamWriter("sample.txt", [System.Text.Encoding]::Default, 4096)
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
我看了看MSDN文档StreamWriter Constructor (Stream, Encoding, Int32),它有三个参数的重载。
有谁知道如何实例化一个System.IO.StreamWriter指定一个编码?
感谢,
第一个ctor参数是流,而不是字符串。 – JohnC 2012-04-20 18:22:13
@JohnC:的确如此。但OP的第一个参数*试图传递的是字符串。这是我的观点。 – 2012-04-20 18:22:48