The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,514 other followers

File extension parameters do include a dot

Posted by jpluimers on 2011/07/20

This is from a long time ago, but still fun:

Sometimes simple things in life are hard do remember.

For instance, I always forgot if a file extension parameter should have a dot in it or not.

Normally it should!

But for clearing an extension, you should use a blank string.

Be aware though that empty extensions look differently depending where in the process you look at them:

C# example:

using System;
using System.IO;
public class Test
{
        public static void Main()
        {
                string extensionLess = Path.ChangeExtension(@"C:\mydir\myfile.com.extension", "");
                Console.WriteLine(extensionLess);
                string extension = Path.GetExtension(extensionLess);
                Console.WriteLine(extension);
        }
}

Outputs:

C:\mydir\myfile.com.

Delphi example:

program Demo;
{$APPTYPE CONSOLE}
uses
  SysUtils;
var
  extensionLess: string;
  extension: string;
begin
  extensionLess := ChangeFileExt('C:\mydir\myfile.com.extension', '');
  Writeln(extensionLess);
  extension := ExtractFileExt(extensionLess);
  Writeln(extension);
end.

Outputs:

C:\mydir\myfile.com
.com

Don’t you love differences in your platforms :)

–jeroen

4 Responses to “File extension parameters do include a dot”

  1. petermoor said

    Interesting indeed…

    Maybe, if file systems would be re-designed completely from scratch, the extension (or let’s say “file type”) should be a completely different field from its name. When you think about it, it’s actually quite silly that we have to parse a name to retrieve the file type, completely based on conventions. And it makes room for problems like this one.

    • jpluimers said

      Actually, back in the DOS 8.3, era it was a field,and not a convention ;-)

      And sometimes the file extensions isn’t all there is; for instance an .xml file can be just XML, but add a [?mso-application progid=”Excel.Sheet”?] (replace [] by angle brackets) in the second line, and the Windows Explorer happily opens it using Excel.

      –jeroen

  2. Pawel said

    Interesting. Shouldn’t the output from the C# example still have two lines?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: