gpt4 book ai didi

c# - 从 cmdlet 中调用 Set-Location

转载 作者:太空宇宙 更新时间:2023-11-03 16:28:12 24 4
gpt4 key购买 nike

我正在为 PowerShell 创建一个 cmdlet,我需要能够从该 cmdlet 中调用 Set-Location(又名 cd)。我会这样做

var setLocation = new Microsoft.PowerShell.Commands.SetLocationCommand();
setLocation.Path = path;
setLocation.Invoke();

除了它给我一个错误,提示 You cannot invoke a cmdlet that derrives from PSCmdlet。我想使用 Set-Location,但我很乐意只更改 shell 的目录。

最佳答案

我能找到的最佳答案是使用 InvokeScript 更改目录:

InvokeCommand.InvokeScript(string.Format("Set-Location {0}", fullPath));

可能有一种“更多 C#”的方法可以做到这一点,但我找不到。

关于c# - 从 cmdlet 中调用 Set-Location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11770973/

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