gpt4 book ai didi

.net - PowerShell 中的某些 .NET 4.0 类似乎不起作用

转载 作者:行者123 更新时间:2023-12-05 00:29:29 26 4
gpt4 key购买 nike

我正在创建一个使用一些 System.Diagnostics.Process 的 powershell 脚本和 System.Diagnostics.ProcessStartInfo类。

这些类完全正常工作,如下所示:

$processInfo = new-object System.Diagnostics.ProcessStartInfo

$processInfo.FileName = "dism.exe"
$processInfo.UseShellExecute = $false
$processInfo.RedirectStandardOutput = $true
$processInfo.Arguments = "/Apply-Image /ImageFile:C:\images\Win864_SL-IN-837.wim /ApplyDir:D:\ /Index:1"

$process = new-object System.Diagnostics.Process
$process.StartInfo = $processInfo

但是,我也想使用 System.IO.StreamReader 类,但是当我尝试使用完全相同的方法时:
$stream = new-object System.IO.StreamReader

或者像这样
$stream = new-object System.IO.StreamReader $process.FileName

我收到错误:
New-Object : Constructor not found. Cannot find an appropriate constructor for type System.IO.StreamReader.
At C:\Users\a-mahint\Documents\Testing\inter.ps1:17 char:21
+ $stream = new-object <<<< System.IO.StreamReader
+ CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

我一直试图弄清楚这个半小时......这是怎么回事?两个类都是 .NET 4.0 的一部分

最佳答案

StreamReader没有无参数构造函数,并且您没有提供任何参数。

在 C# 中尝试一下。你会得到

'System.IO.StreamReader' does not contain a constructor that takes 0 arguments



相反,尝试
$stream = new-object System.IO.StreamReader "foo.txt"

哪里 foo.txt是一个现有的文件。

请注意,它没有参数类型为 ProcessStartInfo 的构造函数。要么,这就是为什么通过 $process.StartInfo没有用。相反,尝试通过 $processInfo.FileName .

关于.net - PowerShell 中的某些 .NET 4.0 类似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17118522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com