如何 运行 C# console app using powershell

How to run C# console app using powershell

我有一个读取 .csv 文件的 C# 控制台应用程序。我想在 powershell 脚本中间 运行 这个。执行此操作的最佳方法是什么?

简单:

Start-Process -Wait PathToFile

如果您不想等待进程完成,请移除“-Wait”开关。

使用 Start-Process,也许与 -PassThru 选项一起使用,例如:

$csvProc = Start-Process my_csv_reader.exe -PassThru

这将允许您稍后执行 $csvProc | Stop-Process 之类的操作,或者稍后在您的脚本中通过 $csvProc.HasExited

检查它是否仍然是 运行

如果你需要更多的控制,你可以这样做:

$csvProc = New-Object System.Diagnostics.ProcessStartInfo

然后您可以像在 C# 中使用 ProcessStartInfo 一样使用 $csvProc(设置文件名、参数、重定向标准输入或输出、启动进程等)。

如果您的可执行文件不在您的路径中,您将需要告诉 powershell 在哪里可以找到它。

& "c:\some path with spaces\foo.exe" <arguments go here>

这个问题之前已经回答过,在这里: Executing an EXE file using a PowerShell script