How do I call overloaded static methods from the .net framework in Powershell?

回眸只為那壹抹淺笑 提交于 2019-12-05 14:09:02

The Path overloads that accept multiple arguments like that are only available in .NET 4 and up. You need to create a config file to tell Powershell to launch using .NET 4, which will give you access to those methods.

Create a file called "powershell.exe.config" in $pshome with the following contents:

<?xml version="1.0"?> 
<configuration> 
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4.0.30319"/> 
        <supportedRuntime version="v2.0.50727"/> 
    </startup> 
</configuration>

To add, issuing the command:

[System.IO.Path]::Combine.OverloadDefinitions

from your shell, you should get the following output:

static string Combine(string path1, string path2)

As you can see, there are no overloads available.

Issue the command:

$PSVersionTable

and look at the CLRVersion - and you will see you are using a version of .net before 4.0, so there are no overloads for Path.Combine available.

You need to create a params array in this case and call the Combine method on it.. The params array can be created as follows: @("C:\", "foo", "bar")

I believe event the following as a parameter "C:\,foo,bar" should call the second method.

I am not sure what you are confused about Path.Combine has two overloaded methods one is combinign two strings and the other a parameter array that accepts a bunch of arguments, the second case in powershell is handled differently than c#.

Hope this answers your question..

FYI, I am a powershell noob..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!