如何在PowerShell cmdlet中获取当前目录?
我正在使用Visual Studio 2010中的C#/ .Net 4.0开发PowerShell 3.0 cmdlet
。我想在PowerShell中获取用户执行cmdlet
的当前目录。 但Directory.GetCurrentDirectory()无法按预期工作。 在下面的代码中,结果是C:\ Users \ Administrator。
问题:使用哪些cmdlet
代码获取PowerShell的当前目录?
[System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Get, "StatusBar")] public class GetStatusBarCommand : System.Management.Automation.PSCmdlet { /// /// Provides a record-by-record processing functionality for the cmdlet. /// protected override void ProcessRecord() { this.WriteObject(Directory.GetCurrentDirectory()); return; } }
PowerShell进程可以具有多个运行空间,因此单个全局目录不适用于PowerShell。 除此之外,在PowerShell中,您当前的目录可能位于注册表中,而不在文件系统中。 但是,您可以使用PowerShell API访问文件系统目录,如下所示:
this.SessionState.Path.CurrentFileSystemLocation