从Powershell调用的DLL中的当前目录错误

ekk*_*kis 2 c# dll powershell

我有一个带有静态方法的DLL,它想知道当前目录.我加载了库

c:\temp> add-type -path "..."
Run Code Online (Sandbox Code Playgroud)

...并调用方法

c:\temp> [MyNamespace.MyClass]::MyMethod()
Run Code Online (Sandbox Code Playgroud)

但是Directory.GetCurrentDirectory()和.Environment.CurrentDirectory得到当前目录错误...

这样做的正确方法是什么?

Mik*_*ray 5

在PowerShell中有两个可能的"目录".一个是进程的当前目录,可通过Environment.CurrentDirectory或获得Directory.GetCurrentDirectory().另一个"目录"是当前Powershell Provider中的当前位置.这是您在命令行中看到的,可通过get-locationcmdlet获得.当您使用set-location(别名cd)时,您正在更改此内部路径,而不是进程的当前目录.

如果您想要一些使用进程当前目录的.NET库来获取当前位置,那么您需要显式设置它:

[Environment]::CurrentDirectory = get-location
Run Code Online (Sandbox Code Playgroud)

Powershell具有可扩展的模型,允许像文件系统中的驱动器一样安装不同的数据源.文件系统只是众多提供商之一.您可以通过其他提供商查看get-psprovider.例如,注册表提供程序允许Windows注册表像文件系统一样进行导航.另一个"功能"让您通过查看所有功能dir function:.