To combine two parts of a file path, you can do
System.IO.Path.Combine (path1, path2);
However, you can't do
System.IO.Path.Combine (path1, path2, path3);
Is there a simple way to do this?
To combine two parts of a file path, you can do
System.IO.Path.Combine (path1, path2);
However, you can't do
System.IO.Path.Combine (path1, path2, path3);
Is there a simple way to do this?
As others have said, in .NET 3.5 and earlier versions there hasn't been a way to do this neatly - you either have to write your own Combine
method or call Path.Combine
multiple times.
But rejoice - for in .NET 4.0, there is this overload:
public static string Combine(
params string[] paths
)
There are also overloads taking 3 or 4 strings, presumably so that it doesn't need to create an array unnecessarily for common cases.
Hopefully Mono will port those overloads soon - I'm sure they'd be easy to implement and much appreciated.
Here's a utility method you can use:
public static string CombinePaths(string path1, params string[] paths)
{
if (path1 == null)
{
throw new ArgumentNullException("path1");
}
if (paths == null)
{
throw new ArgumentNullException("paths");
}
return paths.Aggregate(path1, (acc, p) => Path.Combine(acc, p));
}
Alternate code-golf version (shorter, but not quite as clear, semantics are a bit different from Path.Combine
):
public static string CombinePaths(params string[] paths)
{
if (paths == null)
{
throw new ArgumentNullException("paths");
}
return paths.Aggregate(Path.Combine);
}
Then you can call this as:
string path = CombinePaths(path1, path2, path3);
Aggregate
is an extension method in the System.Linq
namespace, which is in System.Core.dll
. –
Imp return paths.Aggregate(/*path1, */Path.Combine);
–
Predominant Path.Combine
and to try to indicate clearly that at least one path is required for the method to work correctly. Path.Combine
will throw an ArgumentNullException
if either path1
or path2
are null
. But you're absolutely right, it's not totally necessary. –
Imp As others have said, in .NET 3.5 and earlier versions there hasn't been a way to do this neatly - you either have to write your own Combine
method or call Path.Combine
multiple times.
But rejoice - for in .NET 4.0, there is this overload:
public static string Combine(
params string[] paths
)
There are also overloads taking 3 or 4 strings, presumably so that it doesn't need to create an array unnecessarily for common cases.
Hopefully Mono will port those overloads soon - I'm sure they'd be easy to implement and much appreciated.
Not simple, but clever :)
string str1 = "aaa", str2 = "bbb", str3 = "ccc";
string comb = new string[] { str1, str2, str3 }
.Aggregate((x, y) => System.IO.Path.Combine(x, y));
Or:
string CombinePaths(params string[] paths)
{
return paths.Aggregate((x,y) => System.IO.Path.Combine(x, y));
}
EDIT Order23's answer is actually up to date with current .NET https://mcmap.net/q/35542/-in-c-how-do-i-combine-more-than-two-parts-of-a-file-path-at-once
new [] { "aaa", "bbb", "ccc" }.Aggregate (Path.Combine);
(assuming you are using System.IO;
). –
Aglimmer Nope - you have to call Path.Combine()
several times.
You could write a helper method that does it for you, though:
public static string CombinePaths(params string[] paths) {
if (paths == null) {
return null;
}
string currentPath = paths[0];
for (int i = 1; i < paths.Length; i++) {
currentPath = Path.Combine(currentPath, paths[i]);
}
return currentPath;
}
StringBuilder
. I figured performance wasn't an issue here. –
Imp With the method overload introduced in .NET 4 Path.Combine(string [])
Path.Combine(new [] { "abc", "def", "ghi", "jkl", "mno" });
© 2022 - 2024 — McMap. All rights reserved.