Path.Combine vs Parent paths?

  • Thread starter Thread starter Phill W.
  • Start date Start date
P

Phill W.

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.
 
Phill W. said:
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.

Use DirectoryInfo.FullName() to resolve the redirections. Something like:

dim di as new DirectoryInfo("somefolder\..\..\anotherfolder")
dim fixed as string = di.FullName()

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

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
 
Family said:
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.
 
Phill W. wrote:
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.
 
Back
Top