如何在PowerShell cmdlet中获取当前目录?

Bil*_*ble 8 c# powershell c#-4.0

cmdlet在Visual Studio 2010中使用C#/ .Net 4.0 开发PowerShell 3.0 .我想在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
{
    /// <summary>
    /// Provides a record-by-record processing functionality for the cmdlet.
    /// </summary>
    protected override void ProcessRecord()
    {
        this.WriteObject(Directory.GetCurrentDirectory());
        return;
    }
}
Run Code Online (Sandbox Code Playgroud)

Kei*_*ill 22

PowerShell进程可以具有多个运行空间,因此单个全局目录不适用于PowerShell.除此之外,在PowerShell中,您当前的目录可能位于注册表中,而不在文件系统中.但是,您可以使用PowerShell API访问文件系统目录,如下所示:

this.SessionState.Path.CurrentFileSystemLocation
Run Code Online (Sandbox Code Playgroud)