6

If i have lots of directory names either as literal strings or contained in variables, what is the easiest way of combining these to make a complete path?

I know of

Path.Combine
but this only takes 2 string parameters, i need a solution that can take any number number of directory parameters.

e.g:

string folder1 = "foo";
string folder2 = "bar";

CreateAPath("C:", folder1, folder2, folder1, folder1, folder2, "MyFile.txt")

Any ideas? Does C# support unlimited args in methods?

Seb Nilsson
  • 26,200
  • 30
  • 103
  • 130
Gary Willoughby
  • 50,926
  • 41
  • 133
  • 199

4 Answers4

15

Does C# support unlimited args in methods?

Yes, have a look at the params keyword. Will make it easy to write a function that just calls Path.Combine the appropriate number of times, like this (untested):

string CombinePaths(params string[] parts) {
    string result = String.Empty;
    foreach (string s in parts) {
        result = Path.Combine(result, s);
    }
    return result;
}
OregonGhost
  • 23,359
  • 7
  • 71
  • 108
  • @OregonGhost: +1, but switch "" to String.Empty. – user7116 Sep 27 '08 at 23:32
  • According to http://bytes.com/forum/thread453111.html , there is exactly no difference between the two and the Compiler will actually produce exactly the same IL for both. But for readability, I'll change it. – OregonGhost Sep 28 '08 at 12:35
8

LINQ to the rescue again. The Aggregate extension function can be used to accomplish what you want. Consider this example:

string[] ary = new string[] { "c:\\", "Windows", "System" };
string path = ary.Aggregate((aggregation, val) => Path.Combine(aggregation, val));
Console.WriteLine(path); //outputs c:\Windows\System
Kols
  • 3,641
  • 2
  • 34
  • 42
Jason Jackson
  • 17,016
  • 8
  • 49
  • 74
1

I prefer to use DirectoryInfo vs. the static methods on Directory, because I think it's better OO design. Here's a solution with DirectoryInfo + extension methods, that I think is quite nice to use:

    public static DirectoryInfo Subdirectory(this DirectoryInfo self, params string[] subdirectoryName)
    {
        Array.ForEach(
            subdirectoryName, 
            sn => self = new DirectoryInfo(Path.Combine(self.FullName, sn))
            );
        return self;
    }

I don't love the fact that I'm modifying self, but for this short method, I think it's cleaner than making a new variable.

The call site makes up for it, though:

        DirectoryInfo di = new DirectoryInfo("C:\\")
            .Subdirectory("Windows")
            .Subdirectory("System32");

        DirectoryInfo di2 = new DirectoryInfo("C:\\")
            .Subdirectory("Windows", "System32");

Adding a way to get a FileInfo is left as an exercise (for another SO question!).

Jay Bazuzi
  • 45,157
  • 15
  • 111
  • 168
0

Try this one:

public static string CreateDirectoryName(string fileName, params string[] folders)
{
    if(folders == null || folders.Length <= 0)
    {
        return fileName;
    }

    string directory = string.Empty;
    foreach(string folder in folders)
    {
        directory = System.IO.Path.Combine(directory, folder);
    }
    directory = System.IO.Path.Combine(directory, fileName);

    return directory;
}

The params makes it so that you can append an infinite amount of strings.

Path.Combine does is to make sure that the inputted strings does not begin with or ends with slashes and checks for any invalid characters.

Seb Nilsson
  • 26,200
  • 30
  • 103
  • 130
  • Please, let System.IO.Path methods handle things like trimming or adding backslashes. – OregonGhost Sep 27 '08 at 20:56
  • OK, I say it differently. Why re-invent the wheel? Does that hard-coded thing really look more clear to you? What if you're running on a *NIX system with the forward slash being the path separator? What if one of the paths is absolute? Path.Combine handles that as well. – OregonGhost Sep 27 '08 at 21:08
  • Oh common, now you're really just re-inventing Path.Combine. It's there, so use it: http://weblogs.asp.net/rchartier/archive/2006/01/26/436584.aspx – OregonGhost Sep 27 '08 at 21:12
  • Voted down because it is still wrong about Path.Combine, will still fail with absolute paths, handling of wildcards is not clear and is a potentially efficient, but complicated solution. – OregonGhost Sep 27 '08 at 21:38
  • 2
    I'm sorry that I had to insist on this, but I didn't want to let it stand there incorrect. We're supposed to do that, right? My intention was not to hit you personally though. – OregonGhost Sep 27 '08 at 21:59