IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

July Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Using Data   Print This    All Groups  
  From the November 2010 Issue of Prestwood eMag
 
Delphi Using Data:
Set File Extension to Always Show in Explorer
 
Posted 17 years ago on 3/27/2003 and updated 6/8/2008
Take Away: The default setting in Windows Explorer is to always hide known file extensions. This Delphi code shows you how to programatically set your own extension to always show in Explorer.

KB100217

The default setting in Windows Explorer for file extensions is to hide extensions of known file types.

Known file types are those extensions that are shown in the list of File Types in the Windows Explorer Options dialog.

But, let's say that you just built an application that uses a specific file extension for it's data files. Let's say the application is called PixieDust, and you use the .pxy file extension. You want this file extension to appear always, especially in "Open File" dialogs.

If you do this from Windows Explorer, you edit the File Type from Options/File Types, and set:

Description of Type: Pixie File
open=PixieDust.exe (of course, this would include the full path)
"Always show extension" is checked.
 

This last setting adds a Registry flag that will apply to all Windows users.

To programatically set this flag, here is a Delphi function that will update the Registry class for you:

unit ExtensionRegTest;

interface


function CheckExtensionAlwaysShown:boolean;

implementation

uses
Registry, ShlObj;

function CheckExtensionAlwaysShown:boolean;
var
winReg : TRegistry;
  KeyInfo : TRegKeyInfo;
  extClass : string;
  keyFound,
  testFlag : boolean;
begin
  extClass := '';
  keyFound := False;
  testFlag := True;
  winReg := TRegistry.Create(KEY_ALL_ACCESS);
  try
    winReg.RootKey := HKEY_CLASSES_ROOT;
    // NOTE: It's a good idea to open this class key in read-only.
    //       We don't want to accidentally make changes to it!

    keyFound := winReg.OpenKeyReadOnly('.pxy');
    if keyFound then
    begin
      // Test to be sure it is a valid registry key that has values...
      keyFound := winReg.GetKeyInfo(KeyInfo);
      if keyFound then
        // this fetches the Class Name string assigned to the extension's key
        extClass := winReg.ReadString('');
      winReg.CloseKey;
    end;
    if keyFound then
    begin
      // NOTE: I ran into problems in trying to assign the 'AlwaysShowExt' attribute,
      //       until I discovered that opening HKEY_CLASSES_ROOT will ALWAYS set the
      //       access mode to Read-Only!  You have to do this to write a new value!
      winReg.Access := KEY_ALL_ACCESS;
      keyFound := winReg.OpenKey(extClass, False);
      // Test to be sure it is a valid registry key that has values...
      if keyFound then
        keyFound := winReg.GetKeyInfo(KeyInfo);
    end;
    if keyFound then
    begin
      // Look for the setting we're planning on applying.
      testFlag := winReg.ValueExists('AlwaysShowExt');
      if not testFlag then
      begin
        // First, make SURE that the following value is removed if it's there!
        testFlag := winReg.ValueExists('NeverShowExt');
        if testFlag then
        begin
          // NOTE: if we fail to delete the 'NeverShowExt', we don't want to
          //       add the 'AlwaysShowExt' by mistake!  So in the event of a
          //       delete failure, our testFlag will remain TRUE.
          if winReg.DeleteValue(CKEY_HIDE) then
            testFlag := False
          else
            keyFound := False;
        end;
        if not testFlag then
        try
          winReg.WriteString('AlwaysShowExt', '');
        except
          keyFound := False;  // on exceptins, this will indicate an error
        end;
      end;
    end;
  finally
    winReg.CloseKey;
    winReg.Free;
  end;
  if keyFound and (not testFlag) then
    // Finally, we want the Windows Explorer to realize we modified
    // our file type by using the SHChangeNotify API.
    SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
  Result := keyFound;
end;

end.

Comments

1 Comments.
Share a thought or comment...
Comment 1 of 1

Hi,

Thank you for the "File extensions" topic.

I wonder what the CKEY_HIDE constant is : your page is the only one that mentions it on the whole web.

Best regards,

Manuel 

Posted 13 years ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P125A1
Enter key:
KB Post Contributed By Scott Wehrly:

Scott Wehrly is currently working on .Net web applications for the gaming industry. Scott is a former employee of Prestwood Software (he was a Development Manager). Scott's specialties include C#, ASP.Net, MSSQL Server 2005, Delphi, SQL databases, C++, C, and Windows programming in general. When time allows, he participates in this online community.

Visit Profile

 KB Article #100217 Counter
10277
Since 4/2/2008
Sales Website: www.prestwood.com Or visit our legacy sales site: 
legacy.prestwood.com


©1995-2020 Prestwood IT Solutions.   [Security & Privacy]