惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
IT之家
IT之家
博客园_首页
博客园 - 【当耐特】
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
GbyAI
GbyAI
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
H
Help Net Security
T
Tailwind CSS Blog
B
Blog RSS Feed
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
博客园 - 叶小钗
雷峰网
雷峰网
量子位

博客园 - Clark Chan

『iOS小案例』 类型转换失败(use of undeclared identifier) 『iOS学习笔记』 UIButton-1 『iOS学习笔记』 - 变量 属性 方法 实现 『iOS学习笔记』 - Hello Word 博文阅读密码验证 - 博客园 【原创源码】(05):C#-计算绝对路径。 - Clark Chan - 博客园 Windows Vista 睡眠后鼠标死机几分钟 bat批处理文件的执行过程有趣 Windows XP 文件删除之后在回收站找不到文件 Windows XP 从“我的电脑”删除共享文档: Windows XP KB913433补丁(FlashPlayer)无法安装的解决办法 Windows XP 回收站已损坏的解决办法 TreeView右键选中节点。 XmlTextWriter.WriteString与WriteRaw的区别 字符串转换成枚举值 转载:MDA的一些资源的连接 转载:模型驱动架构 C#生成Xml字符串 [转载][求助]在C#中调用JetCar.Netscape的问题?
C#-计算相对路径 - Clark Chan - 博客园
Clark Chan · 2008-07-20 · via 博客园 - Clark Chan

C#-计算相对路径

   /// <summary>
        
/// 计算相对路径
        
/// 后者相对前者的路径。
        
/// </summary>
        
/// <param name="mainDir">主目录</param>
        
/// <param name="fullFilePath">文件的绝对路径</param>
        
/// <returns>fullFilePath相对于mainDir的路径</returns>
        
/// <example>
        
/// @"..\..\regedit.exe" = GetRelativePath(@"D:\Windows\Web\Wallpaper\", @"D:\Windows\regedit.exe" );
        
/// </example>

        public static string GetRelativePath(string mainDir, string fullFilePath)
        
{
            
if (!mainDir.EndsWith("\\"))
            
{
                mainDir 
+= "\\"
            }


            
int intIndex = -1, intPos = mainDir.IndexOf('\\');
          
            
while (intPos >= 0)
            
{
                intPos
++;
                
if (string.Compare(mainDir, 0, fullFilePath, 0, intPos, true!= 0break;
                intIndex 
= intPos;
                intPos 
= mainDir.IndexOf('\\', intPos);
            }


              
if (intIndex >= 0)
            
{
                fullFilePath 
= fullFilePath.Substring(intIndex);
                intPos 
= mainDir.IndexOf("\\", intIndex);
                
while (intPos >= 0)
                
{
                    fullFilePath 
= "..\\" + fullFilePath;
                    intPos 
= mainDir.IndexOf("\\", intPos + 1);
                }

            }

           
            
return fullFilePath;
        }