[Mono-devel-list] Path.Combine, Directory.DirectorySeperatorChar, String.Format...?

Ben Maurer bmaurer at users.sourceforge.net
Wed Feb 4 17:27:48 EST 2004


On Wed, 2004-02-04 at 17:07, Christian Birkl wrote:
> Hi all,
> 
> now that .Net becomes platform independend it raises the question of how
> handling filenames. After reading some of mono sources i found three
> major versions of dealing with the "/" or "\" issues, but which should
> now be used?
> 
> 1. Path.Combine
IMHO, this is the best for two directories, by far. With multiple
directories, it is a performance problem, because you end up creating
multiple string just to throw them away.

> 
> 2. String Concat
The performance here is going to be pretty bad. What is happening is:

String.Concat ("a", Path.DSC.ToString () ...)

However, it is actually worse because before that, you put the DSCs into
an Object [], so you have to box the chars. Also, you have to do a
virtual call on everything, it is a mess.

> 3. String Format:
The performance here is bad because you end up doing a stringbuilder,
and you dont know the exact length.
> 
> 4. Use *only* "/"
Not portable.

What I would do (and I wish MS had done) is to make your own helper
method:

Combine (params string [] args)
{
    string dsc = Path.DSC.ToString ();
    string [] items = new string [1 + (args.Length - 1) * 2];
    int pos = 0;
    foreach (string s in args) {
        if (pos != 0) items [pos++] = dsc;
	items [pos++] = s;
    }
    return String.Concat (items);
}

So you can call:

PathHelper.Combine (foo, bar, baz, do, re, me);

And get the items combined.






More information about the Mono-devel-list mailing list