這篇是對應「C# 絕對路徑轉相對路徑」的程式碼所寫的~
目的就是把轉成相對路徑的位址再轉回成絕對路徑
僅供參考 XD。
private string AbolutePath(string relativePath, string absoluteTo) {
string[] relativeDir = relativePath.Split('\\');
string[] absoluteDir = absoluteTo.Split('\\');
string absolutePath = "";
/* 把 ..\ 配對到絕對路徑中 */
for (int index = 0; index < relativeDir.Length; index++) {
if (relativeDir[index].CompareTo("..") != 0) {
int i = absoluteDir.Length - index; // 停止點
for (int j = 0; j < i; j++)
absolutePath += absoluteDir[j] + "\\";
/* 把剩下的路徑填滿 */
while (index < relativeDir.Length) {
absolutePath += relativeDir[index] + "\\";
index++;
}
absolutePath = absolutePath.Substring(0, absolutePath.Length - 1); // 去除最後的 \
break;
}
}
return absolutePath;
}
沒有留言:
張貼留言