Can Spaces Exist Within A File Extension?

How the filesystem handles names and how the Windows shell (i.e. Explorer) handles file names are two completely different beasts.

The filesystem doesn't care about spaces, dots or anything else -- to it, the filename is just one opaque string (with some restrictions on allowed characters). The name/extension separation is just a made-up convention. The shell, on the other hand, is free to make up its own interpretation of what an extension is because its purpose is not to store and retrieve file information but rather to provide the user with a better experience. So don't go looking there for answers.

I would suggest going with what the System.IO methods return (because following the convention is good), but you can do whatever you like in your code if there's a good reason for it.


The extension on a filename in Windows is purely a convention. The GetExtension and HasExtension methods only look for a dot in the filename and act accordingly. You are free to put spaces anywhere you like within the filename (including the extension).

When you say "Windows thinks so too", it's really just some code in Explorer that tries to parse out extensions, and it simply uses a slightly different algorithm than .NET.


There is no official definition of what an extension is. The common convention is that everything after the final . is the extension.

However if you would grab a HUGE list of all common-used extensions I think you'll only find a handful of examples where spaces in an extension are used.

I would say, disallow spaces in extensions. 999/1000 times the user didn't mean it as an extension.

To quote Wikipedia on filenames:

. (DOT): allowed but the last occurrence will be interpreted to be the extension separator in VMS, MS-DOS and Windows. In other OSes, usually considered as part of the filename, and more than one full stop may be allowed.