Combine and resolve relative paths in C#

The System.IO.Path class provides several static methods for manipulating file paths. The Combine method combines two paths. Unfortunately that method simply concatenates the paths. For example, C:\Data\Test plus ..\data.txt gives C:\Data\Test\..\data.txt, which is probably not what you want.

The Path class's GetFullPath method resolves a path that contains relative elements such as this one and returns an absolute path. In this example, that would be C:\Data\data.txt.

The following code shows how the program combines two paths that you enter.

txtResult.Text = Path.GetFullPath(
Path.Combine(txtPath1.Text, txtPath2.Text));

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.