Sometimes they use Path.DirectorySeparatorChar or even check wether basePath or filename already contains. The program then fails with exceptions like "could not find file c:\Program Files\MyProggie\\settings.xml" or even worse files like MyProggiesettings.xml are created, just because the person configuring the program isn’t aware of how and where to use "\".

This what System.IO.Path.Combine(string, string) does for you:

It validates both paths. They can be null or empty, but if they contain invalid path chars, an argument exception is thrown.

It uses Path.DirectorySeparatorChar to combine, but will never end up having two separators or none.

It takes only the second path if it is absolute, so you never get paths like "c:\data\d:\data\logs\myfile.xml"

If one of the paths is null or empty, the other one is returned.

Simple combinations

If the second path is relative, it will get combined with the first path.

Path.Combine("abc", "file.xml") => "abc\file.xml"

Path.Combine("abc\", "file.xml") => "abc\file.xml"

Path.Combine("c:\abc\", "file.xml") => "c:\abc\file.xml"

Path.Combine("c:\abc\", "data\file.xml") => "c:\abc\data\file.xml"

Path.Combine("", "data\file.xml") => "data\file.xml"

Path.Combine("c:\abc", "") => "c:\abc"

Absolute second path

If the second path is absolute, the first one will be ignored. (See also Path.IsPathRooted(string))

Path.Combine("c:\abc", "\file.xml") => "\file.xml"

(if the second path starts with the DirectorySeparatorChar it is also treated as absolute!)

Path.Combine("c:\abc", "c:\file.xml") => "c:\file.xml"

Example Usage

For NT Services the root directory often is "c:\Windows\System32", so if you want to store something relatively, you should combine it with AppDomain.CurrentDomain.BaseDirectory.

So if you want to write something to a configured file within your application you could code it like this:

I would really like to not have a key to store the value, but I couldn’t avoid. I thought about creating a new Guid per class instance. But this won’t work when you run multiple Applications using some network session state. If someone has an idea on this, please share!

Initializer

Sometimes I need a dictionary or a List<T> to cache some values. So in order to avoid checking wether my SessionVariable has any value before accessing it I would like it to be initialized automatically on first use.

Mads Kristensen is right in his article saying that WE have to wake up for the possibilities the web offers. It’s not about threats, it’s about opportunities!

I have created some Microformats in my current project. But wrongly! Why? Haven’t installed Operator toolbar for Firefox yet. Ridiculous, isn’t it. Or rather laziness.

But that’s what Mads is talking about. We’re just to lazy to look at things our end user could have benefit of. Instead we think we’re ahead others just because auf cool technologies making our live easier.

Actually I don’t think having the functionality in browsers, by add-ons or even natively, is so important. It’s more interesting to use the data in searches and mash-ups. But as soon as it got excepted, there will be a lot of spam, too.