Skip to content

Instantly share code, notes, and snippets.

@zhenlinyang
Last active April 1, 2017 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhenlinyang/181727bfb1013357fca449715c5877e5 to your computer and use it in GitHub Desktop.
Save zhenlinyang/181727bfb1013357fca449715c5877e5 to your computer and use it in GitHub Desktop.
PathCombine
using System;
using System.Collections.Generic;
using System.IO;
public static class PathCombine
{
public static string Combine(params string[] subPath)
{
if (null == subPath || 0 == subPath.Length)
{
return "";
}
string finalPath = subPath[0];
for (int i = 1; i < subPath.Length; i++)
{
finalPath = Path.Combine(finalPath, subPath[i]);
}
finalPath = finalPath.Replace("\\", "/");
return finalPath;
}
public static string PureCombine(params string[] subPath)
{
if (null == subPath || 0 == subPath.Length)
{
return "";
}
string originalPath = Combine(subPath);
var partPaths = originalPath.Split('/');
List<string> partPathList = new List<string>();
for (int i = 0; i < partPaths.Length; i++)
{
if (".." != partPaths[i])
{
partPathList.Add(partPaths[i]);
}
else
{
if (partPathList.Count - 1 < 0)
{
throw new Exception("Stack Overflow");
}
partPathList.RemoveAt(partPathList.Count - 1);
}
}
string finalPath = partPathList[0];
for (int i = 1; i < partPathList.Count; i++)
{
finalPath += "/" + partPathList[i];
}
return finalPath;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment