【发布时间】:2021-12-16 20:12:09
【问题描述】:
在 cmd 中我写了这个并且它可以工作(这个命令从文件中删除 N 第一行):
powershell.exe
$file = "C:\Test\file.txt"
$content = Get-Content $file
$content[10..($content.length-1)]|Out-File $file -Force
我想在 C# 上编写这段代码,但我的方式不正确。你能解释一下原因吗?
using (PowerShell ps = PowerShell.Create())
{
ps.AddCommand($"$file = \"{fullPathToTxt}\"")
.AddCommand("$content = Get-Content $file")
.AddCommand($"$content[{numLine}..($content.length-1)]|Out-File $file -Force")
.Invoke();
}
【问题讨论】:
-
也许通过 C# 执行所有这些操作会更容易?
-
我想通过这种方式来做:D
标签: c# powershell command