How to check if one path is a child of another path?

Unfortunately it's not as simple as StartsWith.

Here's a better answer, adapted from this duplicate question. I've made it an extension method for ease of use. Also using a brute-force catch as just about any method that accesses the file system can fail based on user permissions

public static bool IsSubDirectoryOf(this string candidate, string other)
{
var isChild = false;
try
{
var candidateInfo = new DirectoryInfo(candidate);
var otherInfo = new DirectoryInfo(other); while (candidateInfo.Parent != null)
{
if (candidateInfo.Parent.FullName == otherInfo.FullName)
{
isChild = true;
break;
}
else candidateInfo = candidateInfo.Parent;
}
}
catch (Exception error)
{
var message = String.Format("Unable to check directories {0} and {1}: {2}", candidate, other, error);
Trace.WriteLine(message);
} return isChild;
}

How to check if one path is a child of another path?的更多相关文章

  1. Python os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 与 os.system() 函数

    Python  os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 的区别 os.path.abspath(__file__)返回 ...

  2. fastDFS errcode:9 path:Bad file descriptor errcode:22 path:Invalid argument

    fastDFS errcode:9 path:Bad file descriptor errcode:22 path:Invalid argument <error>status:4 er ...

  3. gyp verb check python checking for Python executable "python2" in the PATH

    缺少python2.7支持 可快速使用以下语句完成安装 npm install --global --production windows-build-tools 到时候会自动下载python的 如果 ...

  4. WPF系列 Path表示语法详解(Path之Data属性语法)

    示例: XAML(代码A): <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ...

  5. Invalid Image Path - No image found at the path referenced under key "CFBundleIconFile": Icon.png

    I got the same error when uploading my app. Moving all icon files to the Asset Catalog works if your ...

  6. Java中path,-classpath,-Djava.library.path的功能和区别

    1. path path是个系统环境变量,声明命令的搜索路径,让操作系统找到指定的工具程序. D:\Program Files\Java\jdk1.8.0_111\bin指定JDK工具路径,例如jav ...

  7. Unity中的Path对应各平台中的Path

    OS: Application.dataPath :                    Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.a ...

  8. App Store Connect Operation Error ERROR ITMS-90032: "Invalid Image Path - No image found at the path referenced under key 'CFBundleIcons': 'AppIcon20x20'"

    1.报错现象 应用提交新包出现报错,切换渠道没问题,但替换回原来的图片资源就出问题了. 明显原因出在图片资源上 2.解决办法 找到原始1024的图片,将图片打开,使用截图工具截图,不要使用另存为的方式 ...

  9. Java-Io之文件File

    File是"文件"和"目录路径名"的抽象表示形式.File之间继承Object,实现了Serializable和Comparable接口,因此文件支持File对 ...

随机推荐

  1. DBA角色职责

    MySQL DBA分架构DBA,运维DBA和开发DBA三种角色,职责介绍如下: MySQL数据库系统日常管理职责 日常管理的主要职责是对MySQL服务器程序mysqld的运行情况进行管理,使数据库用户 ...

  2. 《Semantic Sentence Matching with Densely-connected Recurrent and Co-attentive Information》DRCN 句子匹配

    模型结构 首先是模型图: 传统的注意力机制无法保存多层原始的特征,根据DenseNet的启发,作者将循环网络的隐层的输出与最后一层连接. 另外加入注意力机制,代替原来的卷积.由于最后的特征维度过大,加 ...

  3. JDBC操作数据库步骤

    2018-11-04  20:23:24开始写 1.加载驱动程序(Class.forName) 2.建立连接获取数据库连接对象(DriverManager.getConnection) 3.向数据库发 ...

  4. Robot Framework 教程 (3) - Resource及关键字 的使用

    From:http://www.cnblogs.com/buaawp/p/4754399.html Robot Framework 教程 (3) - Resource及关键字 的使用 在进行软件自动化 ...

  5. cocos 搭建安卓环境

    http://blog.csdn.net/yiye3376/article/details/42219889

  6. gene Ontology (基因本体论)

    gene ontology为了查找某个研究领域的相关信息,生物学家往往要花费大量的时间,更糟糕的是,不同的生物学数据库可能会使用不同的术语,好比是一些方言一样,这让信息查找更加麻烦,尤其是使得机器查找 ...

  7. 设计模式之Template(模板)(转)

    Template定义: 定义一个操作中算法的骨架,将一些步骤的执行延迟到其子类中. 其实Java的抽象类本来就是Template模式,因此使用很普遍.而且很容易理解和使用,我们直接以示例开始: pub ...

  8. 10 分钟速成 Python3

    Python 是由吉多·范罗苏姆(Guido Van Rossum)在 90 年代早期设计. 它是如今最常用的编程语言之一.它的语法简洁且优美,几乎就是可执行的伪代码. 注意:这篇教程是基于 Pyth ...

  9. JustOj 1927: 回文串

    题目描述 回文串是从左到右或者从右到左读起来都一样的字符串,试编程判别一个字符串是否为回文串. 输入 输入一个字符串.串长度<255. 输出 判别输入的字符串是否为回文串,是输出"Y& ...

  10. python smtplib 发送邮件简单介绍

    SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式python的smtplib提供了一种很 ...