| Path.GetFullPath(B).StartsWith(Path.GetFullPath(...

问题描述 投票:4回答:3
Try this:

if (Path.GetFullPath(A).StartsWith(Path.GetFullPath(B)) ||
    Path.GetFullPath(B).StartsWith(Path.GetFullPath(A)))
   { /* ... do your magic ... */ }

is not a , it's a .So you can try this:

If you are not looking for a direct parent-child relationship you can try:

I needed to know if folder B was the same as OR was contained within folder A.

The following worked for me:
c# .net windows path
3个回答
4
投票

我已经尝试过用:

if (!Path.GetFullPath(A).TrimEnd(Path.DirectorySeparatorChar).Equals(Path.GetFullPath(B).TrimEnd(Path.DirectorySeparatorChar), StringComparison.CurrentCultureIgnoreCase)
    && (Path.GetFullPath(A).StartsWith(Path.GetFullPath(B) + Path.DirectorySeparatorChar, StringComparison.CurrentCultureIgnoreCase)
    || Path.GetFullPath(B).StartsWith(Path.GetFullPath(A) + Path.DirectorySeparatorChar, StringComparison.CurrentCultureIgnoreCase)))
   { /* ... do your magic ... */ }
像在

2
投票

C:\files 后期File但这是行不通的。例如,如果我写 "C:\files "和 "C:\files baaa",代码认为 "C:\files baaa "是 "C:\files "的子代,但它不是,它只是C:.当我尝试使用长路径,有大量子代时,问题真的很严重。Directory我也试过用 "if contains \"...但还是不能在所有的路径中工作。

DirectoryInfo A = new DirectoryInfo(Path.GetFullPath("firstPath"));
DirectoryInfo B = new DirectoryInfo(Path.GetFullPath("secondPath"));

if( B.Parent.FullName == A.FullName || A.Parent.FullName == B.FullName )

我可以做什么?

if (Directory
    .GetDirectories(A.FullName,"*",SearchOption.AllDirectories)
    .Contains(B.FullName) ||

     Directory
    .GetDirectories(B.FullName, "*", SearchOption.AllDirectories)
    .Contains(A.FullName))
谢谢!我想确定一个路径是否是另一个路径的子路径。

0
投票

我想确定一个路径是否是另一个路径的子路径。我已经尝试过:如果(Path.GetFullPath(A).StartsWith(Path.GetFullPath(B))

© www.soinside.com 2019 - 2024. All rights reserved.