Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Path.Combine vs Parent paths?

146 views
Skip to first unread message

Phill W.

unread,
Mar 2, 2009, 11:04:57 AM3/2/09
to
Greetings All;

Is there a variation on Path.Combine() that can "boil down" relative
paths that include parent path references ('..\'s) in them?

?Path.Combine("C:\PF\Co\Apps\App1\bin", "..\..\common\data")
"C:\PF\Co\Apps\App1\bin\..\..\common\data"

What I'd /like/ is :

?Path.CombineNCompress("C:\PF\Co\Apps\App1\bin", "..\data")
"C:\PF\Co\Apps\common\data"

Any suggestions?

TIA,
Phill W.

Onur Güzel

unread,
Mar 2, 2009, 3:24:06 PM3/2/09
to
On Mar 2, 6:04 pm, "Phill W." <p-.-a-.-w-a-r...@-o-p-e-n-.-a-c-.-u-k>
wrote:

Hi Phill,
Not sure much but you can first try to get the parent directory of
given path using:
Directory.GetParent(path)

Then try to combine two paths including parent one.

Based on your example in CombineNCompress:
' You need to find parent 2 times because bin's parent is "App1" and
' App1's parent is" Apps" folder
' and specifying "common" as parent of "data" folder

Imports System.IO
Dim parent As String
parent = Directory.GetParent("C:\PF\Co\Apps\App1\bin").Parent.FullName
System.IO.Path.Combine(parent, "common\data")

The above outputs:
"C:\PF\Co\Apps\common\data" by gathering parents of "bin" and "App1"
folders recursively.

Hope it helps,

Onur Güzel


Phill W.

unread,
Mar 3, 2009, 8:40:52 AM3/3/09
to
Family Tree Mike wrote:
>
> Use DirectoryInfo.FullName() to resolve the redirections. Something like:
>
> dim di as new DirectoryInfo("somefolder\..\..\anotherfolder")
> dim fixed as string = di.FullName()
>
> Mike

Excellent - just what I needed!

Many Thanks,
Phill W.

Branco

unread,
Mar 3, 2009, 9:31:41 AM3/3/09
to
Phill W. wrote:
<snip>

> Is there a variation on Path.Combine() that can "boil down" relative
> paths that include parent path references ('..\'s) in them?
>
> ?Path.Combine("C:\PF\Co\Apps\App1\bin", "..\..\common\data")
> "C:\PF\Co\Apps\App1\bin\..\..\common\data"
>
> What I'd /like/ is :
>
> ?Path.CombineNCompress("C:\PF\Co\Apps\App1\bin", "..\data")
> "C:\PF\Co\Apps\common\data"
<snip>

Besides the other suggested approaches, you can also use
Path.GetFullPath:

<example>
Dim P1 As String = "C:\PF\Co\Apps\App1\bin"
Dim P2 As String = "..\..\common\data"
Dim P3 As String = Path.GetFullPath(Path.Combine(P1, P2))
'e.g. C:\PF\Co\Apps\common\data
</example>

It would be useful, I guess, when the path may or may not exist or
when the path contains wildcards.

HTH.

Regards,

Branco.

0 new messages