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.
2 Answers
Use Path.Combine
Dim p = Path.Combine(somePath, "foo\bar")
Documentation: http://msdn.microsoft.com/en-us/library/dd169357.aspx
1 Comment
Jon Skeet
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 :)