zoukankan      html  css  js  c++  java
  • 根据一个绝对路径获取相对路径的方法

       1:  static void Main(string[] args)
       2:  {
       3:      string a = @"c:\a\b\c\d";
       4:      string b = @"c:\a\b\e\f\1.txt";
       5:      int n = 0;
       6:      Console.WriteLine(GetRelativePaths(b, a));
       7:      Console.ReadKey();
       8:  }
       9:   
      10:  public static string GetRelativePaths(string path, string current)
      11:  {
      12:      string a = current.ToLower();
      13:      string b = path.ToLower();
      14:      int i = 0;
      15:      for (; i < Math.Min(a.Length,b.Length); i++)
      16:      {
      17:          if (a[i] != b[i]) break;
      18:      }
      19:      string cur = Regex.Replace(a.Substring(i - 1), @"\\?[a-zA-Z]+:?", @"..\");
      20:      return (cur + path.Substring(i + 1)).Replace(@"\\", @"\");
      21:  }

    输出:

    ..\..\f\1.txt
  • 相关阅读:
    7、单向一对多的关联关系(1的一方有n的一方的集合属性,n的一方却没有1的一方的引用)
    6、JPA_映射单向多对一的关联关系(n的一方有1的引用,1的一方没有n的集合属性)
    解决ubuntu的screen已经处于Attached状态,无法再打开窗口
    关于.ssh出错,无法从远程git仓库拉代码
    给程序添加git commit信息
    ubuntu服务器常用命令
    uint128_t 添加 c++ 重载类型强制转换
    Visual Studio 查看宏展开
    EOS dice移到1.8版本的修改汇总
    ubuntu 添加字体
  • 原文地址:https://www.cnblogs.com/ricksun/p/2658184.html
Copyright © 2011-2022 走看看