Ok, déjà une longue liste de réponses, voici le mien ;-)
Je voulais résoudre ce problème:
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);
Bien sûr, tous les chemins 1-9 doivent contenir une chaîne équivalente à la fin. Voici la méthode PathCombine je suis venu avec:
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);
}
Je pense aussi qu'il est tout à fait gênant que cette chaîne de traitement doit être fait manuellement, je serais intéressé par la raison derrière cela.
OMG c'est tellement stupide qu'il "fonctionne" de cette façon. – Joe
[Il ne change toujours pas dans le noyau .NET.] (Https://github.com/dotnet/coreclr/blob/fb86c0294a999b2c7bd1e13da1fdc0d3c2f701e5/src/mscorlib/shared/System/IO/Path.cs#L189) – zwcloud
@Joe , stupide a raison! En outre, je dois souligner que [la fonction équivalente] (https://nodejs.org/api/path.html#path_path_join_paths) fonctionne très bien dans Node.JS ... Secouant la tête chez Microsoft ... –