10

Visual Studio 2010 で C#/.Net 4.0 を使用して PowerShell 3.0 を開発しています。cmdletユーザーが実行する PowerShell の現在のディレクトリを取得したいと思いますcmdlet。しかし、 Directory.GetCurrentDirectory() は期待どおりに機能しません。以下のコードでは、結果は C:\Users\Administrator になります。

質問: cmdletPowerShell の現在のディレクトリを取得するために使用されるコードは何ですか?

[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;
    }
}
4

1 に答える 1

24

PowerShell プロセスは複数の実行空間を持つことができるため、単一のグローバル ディレクトリは PowerShell では機能しません。それに加えて、PowerShell では、現在のディレクトリがファイル システム内ではなく、レジストリ内にある場合があります。ただし、次のように PowerShell API を使用してファイル システム ディレクトリにアクセスできます。

this.SessionState.Path.CurrentFileSystemLocation
于 2012-12-16T02:26:37.117 に答える