Question:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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:
1 2 3 |
var foobar = Path.Combine(@"C:\", "foo", "bar"); Console.WriteLine(foobar); |
What Powershell will invoke that specific overload? Path.Combine has both of these:
1 2 3 |
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.
Answer:
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:
1 2 3 4 5 6 7 8 |