Ok, schon eine lange Liste von Antworten, hier ist mein ;-)
ich dieses Problem lösen wollte:
string sample1 = "configuration/config.xml";
string sample2 = "/configuration/config.xml";
string sample3 = "\\configuration/config.xml";
string dir1 = "c:\\temp";
string dir2 = "c:\\temp\\";
string dir3 = "c:\\temp/";
string path1 = PathCombine(dir1, sample1);
string path2 = PathCombine(dir1, sample2);
string path3 = PathCombine(dir1, sample3);
string path4 = PathCombine(dir2, sample1);
string path5 = PathCombine(dir2, sample2);
string path6 = PathCombine(dir2, sample3);
string path7 = PathCombine(dir3, sample1);
string path8 = PathCombine(dir3, sample2);
string path9 = PathCombine(dir3, sample3);
Natürlich sollten alle Pfade 1-9 am Ende eine äquivalente Zeichenfolge enthalten. Hier ist die PathCombine Methode, die ich kam mit:
private string PathCombine(string path1, string path2)
{
if (Path.IsPathRooted(path2))
{
path2 = path2.TrimStart(Path.DirectorySeparatorChar);
path2 = path2.TrimStart(Path.AltDirectorySeparatorChar);
}
return Path.Combine(path1, path2);
}
Ich denke auch, dass es sehr ärgerlich ist, dass dieser String-Handling manuell getan werden muss, habe ich im Grund dafür interessieren würde.
OMG das ist so dumm, dass es auf diese Weise "funktioniert". – Joe
[Es ändert sich immer noch nicht im .NET-Kern.] (Https://github.com/dotnet/coreclr/blob/fb86c0294a999b2c7bd1e13da1fdc0d3c2f701e5/src/mscorlib/shared/System/IO/Path.cs#L189) – zwcloud
@Joe Dumm ist richtig! Außerdem muss ich darauf hinweisen, dass [die entsprechende Funktion] (https://nodejs.org/api/path.html#path_path_join_paths) in Node.JS einwandfrei funktioniert ... Kopfschütteln bei Microsoft ... –