Parse File Paths and URLs in a Heartbeat

Make your life easier by using the Path class to work with drive paths, UNC paths, and URLs.

Brian Noyes

October 30, 2009

2 Min Read
ITPro Today logo in a gray background | ITPro Today

asp:feature

LANGUAGES:C#

ASP.NET VERSIONS:1.0 | 1.1

 

Parse File Paths and URLs in a Heartbeat

Make your life easier by using the Path class to workwith drive paths, UNC paths, and URLs.

 

By Brian Noyes

 

If you ever find yourself suspecting a programming taskmight be something programmers often have to do, start looking in the .NETFramework class library for an existing solution. The .NET architects have donean awesome job of including classes in the .NET Framework for most commontasks.

 

Of course, one of the most common tasks programmers haveto perform is extracting portions of a file path or URL to use in theirapplication business logic. Sure enough, the Path class - which lives in theSystem.IO namespace - has static (Shared in VB .NET) methods that let youperform many operations to work with paths that contain drive paths, universalnaming convention (UNC) paths, or URLs. You easily can extract the relevantportions of the path using these methods, or you can construct paths on the flyand let the Path class worry about getting the right path separators in betweenthe parts.

 

The following code demonstrates some of the Path class'smost useful methods. The snippet uses the Debug.Assert method to ensure theresults returned from the Path methods are the ones you expect. It alsodemonstrates the use of the Combine method, which you can use to build up apath from individual parts, but you don't have to worry about providing thepath separators; the Path class looks to see if one is provided already, and itinserts one for you if needed:

 

string filePath = @"C:tempmyappwhatever.config";

Debug.Assert(Path.GetPathRoot(filePath) == @"C:");

Debug.Assert(Path.GetFileName(filePath) ==

       "whatever.config");

Debug.Assert(Path.GetFileNameWithoutExtension(filePath)

        =="whatever");

Debug.Assert(Path.GetExtension(filePath) == ".config");

Debug.Assert(Path.GetDirectoryName(filePath) ==

       @"C:tempmyapp");

string part1 = @"C:temp";

string part2 = "myapp";

string part3 = "whatever.aspx";

string result = Path.Combine(part1,part2);

result = Path.Combine(result, part3);

 

The Path class many other methods besides the ones shownhere. You can specify what the path separator should be (it defaults to "")with the PathSeparator property. You can change the file extension withoutparsing using the ChangeExtension method. You can check what the invalidcharacters are for a path using the InvalidPathChars property.

 

Any time you need to manipulate or parse a path in .NET,first check out this class to see if it can meet your needs. Don't waste timewriting code to detect slashes and dots when you could be writing real businesslogic instead.

 

Brian Noyes is an associate of IDesign Inc. (http://www.idesign.net)and a trainer, consultant, and writer. He's a Microsoft Certified SolutionDeveloper with more than 12 years of programming, design, and engineeringexperience. Brian specializes in .NET architecture, design, and coding ofdata-driven Web and Windows applications, data-access and XML technologies, andOffice automation. He is a contributing editor for asp.netPRO and other publications.E-mail him at mailto:[email protected].

 

 

 

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like