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. KVM_webvirtmgr

    一.webvirtmgr安装前说明: 1:操作做系统:centos7.2_x86_64 2:安装参考出处1:https://github.com/retspen/webvirtmgr/wiki/Ins ...

  2. loadRunner手动关联, web_reg_save_param_regexp()函数正则匹配字符,赋值给变量

    loadRunner写脚本实现登录机票网站,手动关联,获取页面源码中特定字符 手动关联,就是通过函数获取某个步骤生成的字符,赋值给一个变量,这个变量可以作为接下来某个步骤的输入, 以便这个脚本能够在存 ...

  3. Linux基础命令---修改组信息grpmod

    groupmod      修改组的基本信息,包括组名称.组ID等信息.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法 gr ...

  4. 前端框架VUE----指令

    一.什么是VUE? 它是构建用户界面的JavaScript框架(让它自动生成js,css,html等) 二.怎么使用VUE? 1.引入vue.js 2.展示HTML <div id=" ...

  5. axios post参数为空

    今天在360浏览器访问时后台接收不到参数,但是用谷歌浏览器就能收到传入的值.

  6. sql server还原注意事项

    使用Sql Server 2000的数据库备份文件还原Sql Server 2000的数据库和还原Sql Server 2005的数据库区别:1.在还原至Sql 2000时是必须新建数据库并对其还原, ...

  7. 统计py文件中的代码行

    希望是输入一个合法的文件夹的路径,然后代码自动读取该文件夹下的每个py结尾的文件内的代码行数,最后汇总一个数,但现在只是有思路,却没时间写,这是能读取同级文件下的某个文件, with open('te ...

  8. get与post需要注意的几点 (转)

    get与post需要注意的几点 在面试或者笔试时,经常会被问到 HTTP 方法中 get 和 post 的异同点.本文简单整理归纳了一下,以备忘. 1."get/post" VS ...

  9. 桌面版Ubuntu系统固定IP设置和Network-manager设置

    我自己的学校是绑定MAC和IP上网的.这种方法名义上说是为了安全,不过是个搞计算机的都知道随便修改一下MAC就可以使用别人的IP了. 不提蛋疼的事情了,先来说说网络的配置吧 我用的是Ubuntu14. ...

  10. Python-制作抖音图片

    ---------------------------------------------------------------------------------------------------- ...