Last active
April 1, 2017 06:37
-
-
Save zhenlinyang/181727bfb1013357fca449715c5877e5 to your computer and use it in GitHub Desktop.
PathCombine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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