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

  • Email Subscription

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

    Join 717 other subscribers

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:

[sourcecode language=”csharp”]
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);
}
}
[/sourcecode]

Outputs:

[sourcecode language=”text”]
C:\mydir\myfile.com.

[/sourcecode]

Delphi example:

[sourcecode language=”delphi”]
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.
[/sourcecode]

Outputs:

[sourcecode language=”text”]
C:\mydir\myfile.com
.com
[/sourcecode]

Don’t you love differences in your platforms :)

–jeroen

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

  1. petermoor's avatar

    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's avatar

      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's avatar

    Pawel said

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

Leave a Comment