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
  • 相关阅读:
    MVC3、如何应用EntityFramework 连接MySql 数据库 Kevin
    DEV EXPRESS Summary Footer 不显示 Kevin
    装饰模式 Kevin
    Dev 控件 GridControl 控件 二次绑定数据源的问题。 Kevin
    System.InvalidOperationException 异常 Kevin
    LINQ to XML Kevin
    代理模式——代码版“吊丝的故事” Kevin
    VS2012 中的设备 面板 Kevin
    maven 学习笔记(三)创建一个较复杂的 eclipse+android+maven 工程
    maven 学习笔记(一)eclipse+android+maven
  • 原文地址:https://www.cnblogs.com/ricksun/p/2658184.html
Copyright © 2011-2022 走看看