Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Cmdlet中获取当前工作目录_C#_Powershell_Cmdlet - Fatal编程技术网

C# 如何在Cmdlet中获取当前工作目录

C# 如何在Cmdlet中获取当前工作目录,c#,powershell,cmdlet,C#,Powershell,Cmdlet,我正在为C#中的PowerShell编写Cmdlet。我正在子类化Cmdlet,而不是PSCmdlet 有没有办法从PowerShell获取当前目录?我可以使用PSCmdlet使用GetVariableValue(“pwd”)来实现这一点。但是在Cmd类中,我没有可用的 Environment.CurrentDiretory将我指向启动powershell的路径,而不是powershell本身当前所在的位置 编辑 例如: 我通过-say-powershell_ise.exe启动powershel

我正在为C#中的PowerShell编写Cmdlet。我正在子类化
Cmdlet
,而不是
PSCmdlet

有没有办法从PowerShell获取当前目录?我可以使用
PSCmdlet
使用
GetVariableValue(“pwd”)
来实现这一点。但是在Cmd类中,我没有可用的

Environment.CurrentDiretory
将我指向启动powershell的路径,而不是powershell本身当前所在的位置

编辑

例如:

我通过-say-
powershell_ise.exe启动powershell。它在
C:\Windows\System32\WindowsPowerShell\v1.0
中启动。然后,我使用
cd c:\my\folder
更改路径,并运行我的命令
Do Something
。在“Do Something”(C#side)的实现中,我希望能够检索当前路径=>
C:\my\folder


如果可能,我希望避免使用
PSCmdlet

我从
C:\Users\
开始。如果我知道,请输入
cd..
我在
C:\Users\

输入
(获取位置)。Path
返回
C:\Users
。这就是你想要的,不是吗

积极尝试:

WriteObject(this.SessionState.Path.CurrentFileSystemLocation);

参考资料:

我用一个例子更新了我的原始答案。我希望避免运行更多cmdlet来获取当前路径。除非我别无选择。@Hemisphera我已经更新了我的答案。我现在没有访问LinqPad或Visual Studio的权限,因此无法确认此功能是否有效,因为我现在使用的是
PSCmdlet
Cmdlet本身不提供对会话状态的访问。但由于我最终使用了
PSCmdlet
,因此我将此标记为已回答。它比anway更干净。谢谢您确实应该使用
PSCmdlet
,或者从调用cmdlet中将位置作为参数参数传递。我当前正在重写代码以从
PSCmdlet
派生。到目前为止看起来不错。我有点害怕它,因为我真的不需要太多,除了访问它的内部。另一方面。。。这正是它的用途,我想……正是,您有一个运行时依赖关系(当前位置)=使用PSCmdlet