C#中绝对路径转相对路径
来源:互联网 发布:英克灵智医药软件 编辑:程序博客网 时间:2024/06/09 20:02
在一份代码中看到了一个路径转换的方法,贴出来共享。具体是谁的代码也不知道,如有冒犯请留言。
/// <summary> /// 绝对路径转相对路径 /// </summary> /// <param name="strBasePath">基本路径</param> /// <param name="strFullPath">绝对路径</param> /// <returns>strFullPath相对于strBasePath的相对路径</returns> public static string GetRelativePath(string strBasePath, string strFullPath) { if (strBasePath == null) throw new ArgumentNullException("strBasePath"); if (strFullPath == null) throw new ArgumentNullException("strFullPath"); strBasePath = Path.GetFullPath(strBasePath); strFullPath = Path.GetFullPath(strFullPath); var DirectoryPos = new int[strBasePath.Length]; int nPosCount = 0; DirectoryPos[nPosCount] = -1; ++nPosCount; int nDirectoryPos = 0; while (true) { nDirectoryPos = strBasePath.IndexOf('\\', nDirectoryPos); if (nDirectoryPos == -1) break; DirectoryPos[nPosCount] = nDirectoryPos; ++nPosCount; ++nDirectoryPos; } if (!strBasePath.EndsWith("\\")) { DirectoryPos[nPosCount] = strBasePath.Length; ++nPosCount; } int nCommon = -1; for (int i = 1; i < nPosCount; ++i) { int nStart = DirectoryPos[i - 1] + 1; int nLength = DirectoryPos[i] - nStart; if (string.Compare(strBasePath, nStart, strFullPath, nStart, nLength, true) != 0) break; nCommon = i; } if (nCommon == -1) return strFullPath; var strBuilder = new StringBuilder(); for (int i = nCommon + 1; i < nPosCount; ++i) strBuilder.Append("..\\"); int nSubStartPos = DirectoryPos[nCommon] + 1; if (nSubStartPos < strFullPath.Length) strBuilder.Append(strFullPath.Substring(nSubStartPos)); string strResult = strBuilder.ToString(); return strResult == string.Empty ? ".\\" : strResult; }
用法:
using System;using System.Text;using System.IO;namespace PathDemo{class MainClass{public static void Main (string[] args){string str = GetRelativePath ("/Users/apple/Documents/TempDemo/PathDemo", "/Users/apple/Documents/TempDemo/PathDemo/PathDemo/Main.cs");Console.WriteLine ("********");Console.WriteLine (str);Console.WriteLine ("********");}public static string GetRelativePath(string strBasePath, string strFullPath){if (strBasePath == null)throw new ArgumentNullException("strBasePath");if (strFullPath == null)throw new ArgumentNullException("strFullPath");strBasePath = Path.GetFullPath(strBasePath);strFullPath = Path.GetFullPath(strFullPath);var DirectoryPos = new int[strBasePath.Length];int nPosCount = 0;DirectoryPos[nPosCount] = -1;++nPosCount;int nDirectoryPos = 0;while (true){nDirectoryPos = strBasePath.IndexOf('\\', nDirectoryPos);if (nDirectoryPos == -1)break;DirectoryPos[nPosCount] = nDirectoryPos;++nPosCount;++nDirectoryPos;}if (!strBasePath.EndsWith("\\")){DirectoryPos[nPosCount] = strBasePath.Length;++nPosCount;} int nCommon = -1;for (int i = 1; i < nPosCount; ++i){int nStart = DirectoryPos[i - 1] + 1;int nLength = DirectoryPos[i] - nStart;if (string.Compare(strBasePath, nStart, strFullPath, nStart, nLength, true) != 0)break;nCommon = i;}if (nCommon == -1)return strFullPath;var strBuilder = new StringBuilder();for (int i = nCommon + 1; i < nPosCount; ++i)strBuilder.Append("..\\");int nSubStartPos = DirectoryPos[nCommon] + 1;if (nSubStartPos < strFullPath.Length)strBuilder.Append(strFullPath.Substring(nSubStartPos));string strResult = strBuilder.ToString();return strResult == string.Empty ? ".\\" : strResult;}}}
0 0
- C#中绝对路径转相对路径
- C#相对路径转绝对路径,绝对路径转相对路径
- C# 中相对路径、绝对路径的问题
- C# 中相对路径、绝对路径的问题
- 相对路径转绝对路径
- URL相对路径转绝对路径
- URL相对路径转绝对路径
- C# 相对路径转绝对路径
- 将相对路径转绝对路径
- C# 将绝对路径转换为相对路径
- C#中的绝对路径和相对路径问题
- c# winform 绝对路径与相对路径
- [转]java(Web)中相对路径,绝对路径问题总结
- java(Web)中相对路径,绝对路径问题(转)
- java(Web)中相对路径,绝对路径问题总结【转】
- 【转】java(Web)中相对路径,绝对路径问题
- 【转】java中相对路径,绝对路径问题总结
- java中相对路径,绝对路径问题总结(转)
- java cookie
- USACO2008Feb T3麻烦的聚餐
- 链表之单、双链表反序
- 汇编跳转指令
- iOS模拟器上home键调用方法(2种)
- C#中绝对路径转相对路径
- Linux w 命令
- ios调用web service
- iOS资源图片命名注意事项
- Objective C(OC)面向对象三大特性之:封装
- 堆排序问题
- CF#200 div2 B Simple Molecules(Brute force)
- 多表查询的性能很低
- Spring动态创建,加载,使用多数据源