【问题标题】:System.Diagnostics.ProcessStartInfo hangs if the arguments include a tilde mark如果参数包含波浪号,则 System.Diagnostics.ProcessStartInfo 挂起
【发布时间】:2019-10-03 18:10:47
【问题描述】:

git 命令git reset HEAD~ 将撤消最后一次提交。但是,当我尝试使用System.Diagnostics.ProcessStartInfo 在 PowerShell 中运行命令时,该进程就挂起。

我认为它与波浪号 (~) 标记有关,因为我试图用反斜杠转义它,并且命令不再挂起。但它也没有奏效。它只是默默地失败了。

这是脚本:

$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "C:\Program Files\Git\cmd\git.exe"
$pinfo.Arguments = "reset HEAD~"
$pinfo.WorkingDirectory = "C:\GitRepo"
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.CreateNoWindow = $true

$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()

【问题讨论】:

    标签: git powershell escaping freeze tilde


    【解决方案1】:

    将参数作为列表传递似乎可以解决问题:

    $pinfo.Arguments = "reset","HEAD~"
    

    我猜这个方法还有更多的自动转义。

    【讨论】:

      猜你喜欢
      • 2012-03-08
      • 1970-01-01
      • 2012-11-06
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 2020-07-22
      相关资源
      最近更新 更多