protected static string Canonicalize(string path)
{
if (Path.DirectorySeparatorChar != Path.AltDirectorySeparatorChar)
path = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
string leadingSeparators = "";
foreach (char c in path)
{
if (c == WindowsDirectorySeparatorChar || c == NonWindowsDirectorySeparatorChar)
{
leadingSeparators += Path.DirectorySeparatorChar;
}
else break;
}
#if (CLR_2_0x || CLR_4_0) && !NETCF
string[] parts = path.Split(DirectorySeparatorChars, StringSplitOptions.RemoveEmptyEntries);
#else
string[] parts = path.Split(DirectorySeparatorChars);
#endif
int count = 0;
bool shifting = false;
foreach (string part in parts)
{
switch (part)
{
case "":
case ".":
shifting = true;
break;
case "..":
shifting = true;
if (count > 0)
--count;
break;
default:
if (shifting)
parts[count] = part;
++count;
break;
}
}
return leadingSeparators + String.Join(Path.DirectorySeparatorChar.ToString(), parts, 0, count);
}