2016-04-11 78 views
0

我正在C#中實現一個自定義參數完成器,從IArgumentCompleter繼承。在CompleteArgument,我嘗試做的如何在IArgumentCompleter的C#實現中獲取當前文件系統的位置?

$ExecutionContext.SessionState.Path.CurrentFileSystemLocation.ProviderPath 

相當於我試圖

Runspace.DefaultRunspace.SessionStateProxy.Path.CurrentFileSystemLocation.Path 

而是拋出異常告訴我管道已經運行。

這樣做的正確方法是什麼?

+1

'EngineIntrinsics EI;使用(PowerShell ps = PowerShell.Create(RunspaceMode.CurrentRunspace)){ei = ps.AddScript(「$ ExecutionContext」)。調用()[0]}' – PetSerAl

回答

0

從@PetSerAl註釋是一個答案:

string pwd; 
EngineIntrinsics ei; 
using(PowerShell ps = PowerShell.Create(RunspaceMode.CurrentRunspace)) 
{ 
    ei = ps.AddScript("$ExecutionContext").Invoke<EngineIntrinsics>()[0] 
    pwd = ei.SessionState.Path.CurrentFileSystemLocation.ProviderPath 
} 
相關問題