1

I have developed a C# WPF application that will display .pdf files to the user. The user can load the PDF directly

File --> Load --> (select PDF file) --> Open

after selecting the PDF, and pressing open, the PDF is displayed from inside the application.

Or they can open a ZIP folder that contains a single PDF file, and the PDF inside that ZIP will automatically be shown to the user from inside the application

File --> Load --> (select ZIP folder) --> Open

after selecting the ZIP, (it is known that the contents of the zip consists entirely of one single PDF), and pressing open, the PDF inside that ZIP is displayed from inside the application (in the same way as just opening a PDF directly).

I now want to modify my program so that it can open a 'folder' that has a custom extension- i.e. I want to create a folder, similar to a ZIP- containing a single PDF file, but the extension of that folder should be something I choose, rather than .zip, for example, so that I could have a compressed folder called folder.abc, containing a single PDF xyz.pdf.

I then want to associate the extension .abc with my application, so that any time the user double clicks a .abc file/ folder, it is automatically opened by my application.

What I don't know, is how I can create custom extension and use it? For example, how could I change folder.zip to folder.abc, and have anything with a .abc extension automatically assigned to be opened by my application when a user double clicks it?

Edit

Ok, so I can create custom files with my own extension by creating a .txt file, opening it, and then saving it again, setting its 'type' to All Files, and giving it a name that ends in .abc, i.e. customFile.abc, and I can associate my extension with my application by right clicking it, going to 'Open with', and selecting my own application.

I can also do this with a ZIP folder in the same way.

However, when I currently open any of the files/ folders that I have given my custom extension to, they just open my application with a blank window, i.e. the .pdf files that they contain are not displayed- I have to then use my application window to open them. I do this by selecting File --> Open and choosing the .abc file that I have just double clicked on to open my application window, and it then displays the PDF that this .abc file contains...

Why does it not automatically display the contained .pdf when I double click on it from within windows explorer?

  • 2
    Do you want to do that manually, or should your program do it when installing etc.? Latter option => Stackoverflow. – deviantfan Jun 10 '16 at 14:46
  • Google ftype and assoc – Marged Jun 10 '16 at 14:49
  • Manually to start off with- just to get an understanding of how to use custom file extensions. When I have a clearer understanding of that, and my program is able to successfully read a file/ folder that I have given a custom extension to, then I'll start to think about getting my program to set that up when it's being installed. – Noble-Surfer Jun 10 '16 at 14:49
  • 1
    Create a file of the required type. Then in Explorer right-click on the file you created, and select Properties and use Opens with to define your program. – AFH Jun 10 '16 at 15:01
  • About a more configurable solution: Windows had it, but in newer versions it's not present (as GUI) anymore. Either registry editing, or code is necessary (or a thirdparty program, there are some) – deviantfan Jun 10 '16 at 15:06
3

There are various ways this can be done. For instance, you can muck with the registry using standard registry-editing tools. However, there is also a way to affect this using the command line. As that is likely easier for automation, that is a good way to demonstrate this. Since your question showed up a made up extension of .abc, I use that.

From a command line, get to a command prompt. If you have UAC enabled, this needs to be an elevated command prompt.

First, create a filetype. e.g.:

ftype zzzfile=C:\Windows\System32\Notepad.exe %%1

Then, view your results:

ftype zzzfile
reg query HKCR\zzzfile\SHELL\OPEN\COMMAND /ve

Notes:

  • When creating this file type, the "%%1" is meant to escape the percent sign. The two percent signs become one. So when you view the results, it will simply show "%1" instead of "%%1".
  • When making the file type, specifying the entire executable name is required. Things won't work well if you just said "Notepad". (By "things won't work well", I mean that Windows will ask the user what to do with the extension.) The C:\Windows\System32\ actually did seem optional, but the ".exe" did not.

Then, associate an extension to your file type. Again, this modifies your system behavior, so if UAC is enabled then get to an elevated command prompt. Run:

assoc .abc=zzzfile

Now, from that command prompt, or an un-elevated one, you can check your results:

assoc .abc
start filename.abc

(Tested with Windows 7 x64.)


Note: During preliminary testing, I mucked with the GUI. That prevented usage of FTYPE and ASSOC from working properly, with the same extension.

Detecting the problem was simple enough. Fixing it wasn't.

I was able to find the problem using this:

REG QUERY HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc /s

Tragically, this did NOT work to delete it:

REG DELETE HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc
REG DELETE HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc\UserChoice /v Progid

I confirmed the problem with a graphical program: REGEDIT on UserChoice, then Edit, "Permissions...", (my user name), Advanced, (Permissions tab) showed I had a DENY, type Special. Edit... from there showed "Set Value" had Deny.

A way to fix that from the command line appears to be REGINI, from the NT 4 resource kit. To do things this way, I needed to figure out my SID.

wmic USERACCOUNT Get Name,SID (Find my account. The SID starts with "S-".)

Make a text file. I decided to call it "fix.txt".

KB 254031 indicated the first line is "\Registry\User\" followed by the user's SID. So the text file will start with that.

Despite some of the examples on various Microsoft KB articles, the syntax can be as short as a single line, as shown (but needing customization...)

\Registry\User\S-#-#-##-##########-##########-##########-1000\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc\UserChoice [1 5 7 17]

(Naturally, adjust the ".abc" to whatever extension you're trying to fix. The #'s also need to be replaced, so that a valid SID is used.)

Then, use that text file with: REGINI fix.txt

Now that the key isn't preventing us from making changes, we can delete the UserChoice key's Progid value. Actually, go ahead and delete the entire key related to this extension.

REG DELETE HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc

Then, this registry key was no longer overriding, so the results of FTYPE and ASSOC were taking effect as desired.

| improve this answer | |
  • Hey, thanks for your answer. If I try running the first command you gave: ftype zzzfile=C:\Windows\System32\Notepad.exe %%1, I get a message that says: File type 'zzzfile' not found or no open command associated with it. Is that why I need to enable UAC? What is UAC and how do I enable it? – Noble-Surfer Jun 10 '16 at 15:25
  • I confirmed, that is the error message if running from a command prompt that is affected by UAC, without the command prompt being elevated. UAC stands for User Account Control. UAC comes enabled by default. Try right-clicking on the icon for your command prompt and see if you have a "Run As Administrator" option. (Note: This term "Administrator" is not referring to your Administrator account. It refers to the Administrator/Elevated level of permissions in UAC.) – TOOGAM Jun 10 '16 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.