How can Path.Combine be used with more than two arguments?

前端 未结 3 1714
孤城傲影
孤城傲影 2021-02-18 14:02

I\'m surprised there\'s not an overload that can take a string array. Anyway, what is the best way to avoid nesting calls to Path.Combine?

pathValue = Path.Combi         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-18 14:07

    The efficiency side of things isn't the problem IMO - it's the usability side of things. Personally I think there ought to be an overload of:

    Combine(string first, string second, string third, params string[] others)
    

    You need to have at least three to prevent it from clashing with the existing two-parameter version if you just write Path.Combine("foo", "bar") but it would certainly help to make code clearer. Why not open a feature request on Connect?

    Of course, you can implement this yourself (and in another class the number of parameters doesn't matter so much):

    public static string CombinePaths(string first, params string[] others)
    {
        // Put error checking in here :)
        string path = first;
        foreach (string section in others)
        {
            path = Path.Combine(path, section);
        }
        return path;
    }
    

提交回复
热议问题