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. workerman 7272端口被占用

    1/问题:workerman 7272端口被占用 2/策略: 1.查找被占用的端口 netstat -tln netstat -tln | grep 8083 netstat -tln 查看端口使用情 ...

  2. jQuery筛选--first()和last()

       first() 概述 获取匹配的第一个元素    last() 概述 获取匹配的最后个元素 <!DOCTYPE html> <html> <head> < ...

  3. python isinstance()方法的使用

    1. 描述Python中的 isinstance() 函数,是Python中的一个内置函数,用来判断一个函数是否是一个已知的类型,类似 type(). 2. 语法isinstance(object,c ...

  4. django 常用方法总结 < 手写分页-上传头像-redis缓存,排行 ...>

    1.不使用自带模块<Paginator>的手写分页功能views.pydef post_list(request): page = request.GET.get('page', 1) # ...

  5. Flask的请求对象--request

    request-Flask的请求对象 请求解析和响应封装大部分是有Werkzeug完成的,Flask子类化Werkzeug的请求(Request)对象和响应(Response)对象,并添加了和程序的特 ...

  6. 通过百度api 获取当前城市3种方法

    方法一:function showLocation(data) { console.log(data.content); //alert(data.content.address_detail.cit ...

  7. Python中*args和**kwargs 的简单使用

    # 在函数定义中使用*args和kwargs传递可变长参数. *args用作传递非命名键值可变长参数列表(位置参数); kwargs用作传递键值可变长参数列表# *args表示任何多个无名参数,它是一 ...

  8. 费马小定理与GCD&LCM

    若 t = 1 ,  a ^ ( p - 2 ) 为 a 在取模 p 意义下的乘法逆元 通常用 inv 表示 证明: b * a =(三等)1(mod p) a ^ ( p - 2 ) * a =(三 ...

  9. sqlserver搜索中怎么把varchar类型转换成numeric类型

    sqlserver搜索中怎么把varchar类型转换成numeric类型 可以用cast来转换 如:列名叫grade,表名为A select cast(grade as numeric(y,x)) f ...

  10. Mysql初级第三天(wangyun)

    1.JDBC简介 1).数据库驱动 2).SUN公司为统一数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC. 3).JDBC全称:Java Database Connectivity( ...