在 C# 中访问 Powershell 参数

Accessing Powershell Arguments in c#

我有一个简单的 c# 控制台应用程序,我是 运行 通过 Powershell ps1 文件。我想在调用脚本时获取通过 $args[0] 传递的参数,并在我的 c# 应用程序中的方法中使用它。我发现了很多相反的东西,但对我想做的事情没有任何帮助。任何输入将不胜感激。这大致是我最近尝试过的。谢谢!

$filePath = $args[0]
$Check = New-Object -ComObject "Take c# file and try to make into    object"
$Check.hasFile($filePath)
Start-Process -FilePath "Path to c# exe"

.hasFile 是我试图将 Powershell 参数传递给的 c# 方法。

如果我的理解正确,您只是想将脚本变量作为参数传递到您的 C# 应用程序中?如果是这样,您可以让 Program.csMain 方法接受一个字符串数组,然后在启动应用程序时将脚本变量传递给 Start-Process

$myargs = @($args[0],$myvariable, .... <whatever>)
Start-Process MyCSharpApplication.exe $myargs

希望对您有所帮助!

编辑:

澄清一下,在您只有一个要传入的变量的简单情况下,您甚至不需要单独的数组变量。你可以这样做:

Start-Process MyCSharpApplication.exe $myvariable