如何检查一条path是否是另一条path的孩子?
只是检查子string是不是要走的路,因为可以有项目,如。 等等
非托pipepipe理(.Net)
Windows / .NET的方式来监视/debugging串口?
如何通过Windows应用程序或Web应用程序调用代码来识别代码
在iis中的IP地址和域名限制通过PowerShell
在GetResponse之前将HttpWebRequest视为string,而不使用fiddler
这将是一种方法,你有路径A和B,使用Path.GetFullPath()函数将它们转换为完整路径。 接下来检查一个完整路径是否是另一个的起始子字符串。
所以那将是
if (Path.GetFullPath(A).StartsWith(Path.GetFullPath(B)) || Path.GetFullPath(B).StartsWith(Path.GetFullPath(A))) { /* ... do your magic ... */ }
不幸的是,它不像StartsWith那么简单。
这是一个更好的答案,从这个重复的问题改编。 我已经使它成为易用性的扩展方法。 另外,使用暴力catch作为访问文件系统的任何方法都可能会因用户权限而失败。
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; }
任何基于字符串的解决方案都可能受到目录遍历攻击或正斜线后缀等问题的影响。 不幸的是,.NET Path类不提供这个功能,但Uri类以Uri.IsBaSEOf()的形式提供。
Uri potentialBase = new Uri(@"c:dir1"); Uri regular = new Uri(@"c:dir1dir2"); Uri confusing = new Uri(@"c:temp..dir1dir2"); Uri malicIoUs = new Uri(@"c:dir1..windowssystem32"); Console.WriteLine(potentialBase.IsBaSEOf(regular)); // True Console.WriteLine(potentialBase.IsBaSEOf(confusing)); // True Console.WriteLine(potentialBase.IsBaSEOf(malicIoUs)); // False
在C#中,你可以这样做:
string cp = Path.GetFullPath(childpath); string pp = Path.GetFullPath(parentPath); if(pp.StartsWith(cp)) return true; else return false;
有同样的问题。 如果您将路径设置为字符串,则可以使用StartWith()
if (pathA.StartsWith (pathB + "\")) {
虽然我不确定它是否是跨平台的,但是它能在PC上运行
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。