tags:

views:

68

answers:

2

Is there an easy way of dynamically building a filepath in .Net? At the moment I'm building the filepath by concatenating various strings (from application settings, user input and Date.ToString) but this relies on there not being double '\' characters or illegal characters etc in the strings. Obviously I can manually validate the strings for this sort of thing but I was wondering if there was something built into .Net that can handle this.

+3  A: 

System.IO.Path.Combine()
This class has many members related to Path manipulation

Gishu
+10  A: 

Use Path.Combine

Dim p = Path.Combine(somePath, "foo\bar")

Documentation: http://msdn.microsoft.com/en-us/library/dd169357.aspx

JaredPar
Preferably Path.Combine(Path.Combine(somePath, "foo"), "bar") IMO :) (Why isn't there an overload of Path.Combine which takes more strings? Please nag the BCL team for me :)
Jon Skeet
I've wondered about that for a long time.
Kev