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

Christian Birkl wingman at techmonkeys.org
Wed Feb 4 17:07:58 EST 2004


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

	Usage:
		string path = Path.Combine("home", "username");

	Advantage(s):
		No need to mess around with Path.DirectorySeparatorChar.
	Disadvantage(s)
		Looks (and performes?) somewhat ugly when combining
		a depth directory structure like:
			/usr/local/some/foo/doc

		string path = Path.Combine("usr", "local");
		path = Path.Combine(path, "some");
		path = Path.Combine(path, "foo");
		path = Path.Combine(path, "doc");
		/* ... */

2. String Concat

	Usage:
		string path = "home" + Path.DirectorySeparatorChar +
"username";

	Disadvantage(s):
		A lot of usage of Path.DirectorySeperatorChar which
makes the code
		more unreadable:

		string path = "usr"
				+ Path.DirectorySeperaterChar
				+ "local"
				+ Path.DirectorySeperaterChar
				+ "some"
				+ Path.DirectorySeperaterChar
				+ "foo"
				+ Path.DirectorySeperaterChar
				+ "doc"
				+ /* ... */;
3. String Format:

	Usage:
		string path = String.Format("home{0}username");

	Advantage:
		Makes the code smaller than using
Path.DirectorySeperatorChar all over
	Disadvantages:
		Less slower than Concat (i guess)

		string path =
String.Format("usr{0}local{0}some{0}foo{0}doc{0}...",
Path.DirectorySeperaterChar);

4. Use *only* "/"

	Usage:

		string path = "home/username";

	Disadvantages:

		Works (not?) on all platforms (does Win95 understand
'/'? I know NT+ do.)


To sum up, is there any conventation in mono or which version (i bet the
one i didn't mention ;-)) is the best to be most portable?

Greets,

	Christian




More information about the Mono-devel-list mailing list