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));


Comments