Little Known, Invaluable Methods and Properties in the .NET Framework Base Class Library : Working with File PathsBy Scott Mitchell
There was a simpler time in history when one could know all intricacies in their given field. An experienced cobbler in yesteryear could feasibly know all there is to cobbling with the materials they had available. With the depth and breadth of knowledge today, however, such mastery of any given topic is virtually impossible, requiring people to tightly focus in on particular aspects of a given field. For example, if you are reading this article you are likely a Web developer using Microsoft's Web technologies. From a big picture view that encompasses the totality of the field of computer science, you are working in a very specific niche.
Even within this particular, focused realm of ASP.NET development, the breadth and depth of tools available are still astounding and overwhelming. I've used Visual Studio .NET countless times over the years and still am finding out new tips and tricks. (See Getting the Most Out of Visual Studio .NET for a list of some of my favorite tips and productivity enhancements!) Additionally, the .NET Framework Base Class Library (BCL) contains a slew of useful methods that, I've found, are beneath the radar for most ASP.NET developers.
This article provides a listing of those .NET Framework BCL methods and properties that are quite handy but not well-known. I'd like this article to be a work in progress, so if you know of any .NET Framework methods or properties you consider not in every ASP.NET developer's lexicon, but should be, let me know! Each week I plan to add additional useful methods and properties focused in a particular area. This week's tips and tricks focus on working with file paths. Read on to learn more!
I plan on adding additional topics to this list. The first topic is Working with File Paths. (If you have any suggested methods or properties that you think are not well-known, but invaluable, please be sure to let me know.)
|Little Known, Invaluable Methods and Properties in the .NET Framework BCL|
|Working with File Paths||(Working with Colors) Next -->|
The .NET Framework contains a
Visit the Technical Docs for the
Combines two paths together, returning a single string path. This is useful if you have a directory path and
a file path, and you want to combine them into a single, complete path. For example, imagine you have the string
Rather than having to futz around with the various permutations, adding or removing backslashes, just use
Be sure to heed the Remarks section of the
If path1 does not end with a valid separator character as defined in DirectorySeparatorChar, AltDirectorySeparatorChar, or VolumeSeparatorChar, DirectorySeparatorChar is appended to path1 before concatenation.So, if the second input parameter starts with a
Have a filepath and want to know the extension of the file? The
At times you'll have a complete path but are only interested in the file name (not the directory it exists in).
|~ and Control.ResolveUrl(string)||
If, from an ASP.NET page, you need to access or refer to a file that resides in a different directory, you have a number
of options to refer to that file. You can use an absolute path, such as
The problem with either of these approaches is that they are sensitive to changes in the site's layout or configuration. With absolute paths you can run into difficulties if you use different base application directories for development and production (i.e., using a virtual directory as the application root in a development server, but hosting the live version of the site in the root directory of the website). With relative paths things can go haywire if the ASP.NET page that references the file is moved to another directory.
A workaround for this is to use the
There are a slew of methods and properties in the .NET Framework Base Class Library that, while quite useful and versatile, are not well as well-known among the ASP.NET developer community as they ought to be. This article is an attempt in providing sets of methods and properties that most ASP.NET developers will find quite useful in real-world situations. If you have any recommendations on methods or properties that you find invaluable, but that you noticed not many folks know about, please don't hesitate to let me know.