using System;
using System.Collections.Generic;
using System.IO;
class Program
{
static void Main(string[] args)
{
string txt = "/home/tester/bin/x64/Debug/../test.conf";
NoException(Path.GetFullPath, txt);
NoException((path) => new Uri(path).LocalPath, txt);
NoException(NormalizePath, txt);
}
private static void NoException(Func<string, string> normalizePath, string path)
{
try
{
Console.WriteLine(normalizePath(path));
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine();
}
internal static string NormalizePath(string path)
{
List<string> pathList = new List<string>();
string[] parts = path.Split(new char[] { '/', '\\' }, StringSplitOptions.None);
foreach (string part in parts)
{
if (part == ".")
{
continue;
}
if (part == ".." && pathList.Count >= 1)
{
pathList.RemoveAt(pathList.Count - 1);
continue;
}
pathList.Add(part);
}
return string.Join('/'.ToString(), pathList.ToArray());
}
}