4

Below is a transcript of what I've tried and what happens.

I'm looking for how to call a specific overload along with an explanation of why the following does not work. If your answer is "you should use this commandlet instead" or "call it twice" please understand when I don't accept your answer.

PS C:\> [System.IO.Path]::Combine("C:\", "foo")
C:\foo
PS C:\> [System.IO.Path]::Combine("C:\", "foo", "bar")
Cannot find an overload for "Combine" and the argument count: "3".
At line:1 char:26
+ [System.IO.Path]::Combine <<<< ("C:\", "foo", "bar")
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

PS C:\> [System.IO.Path]::Combine(, "C:\", "foo", "bar")
Missing ')' in method call.
At line:1 char:27
+ [System.IO.Path]::Combine( <<<< , "C:\", "foo", "bar")
    + CategoryInfo          : ParserError: (CloseParenToken:TokenId) [], Paren
   tContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall

PS C:\> [System.IO.Path]::Combine($("C:\", "foo", "bar"))
Cannot find an overload for "Combine" and the argument count: "1".
At line:1 char:26
+ [System.IO.Path]::Combine <<<< ($("C:\", "foo", "bar"))
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

this is what I do in c#, which works:

var foobar = Path.Combine(@"C:\", "foo", "bar");
Console.WriteLine(foobar);

What Powershell will invoke that specific overload? Path.Combine has both of these:

public static string Combine (string path1, string path2, string path3);
public static string Combine (params string[] paths);

Is it possible to call both of these, or just one? Obviously, in this specific case, it's difficult to tell the difference.

1
  • 3
    Those overloads are new in .Net 4.0. Are you sure Powershell is running under .Net 4.0? You can use [environment]::version to get the CLR version. Commented Sep 21, 2012 at 21:54

3 Answers 3

7

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Or you can install and use powershell 3.0 which runs on .NET 4.0 already.
4

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.

Comments

1

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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.