views:

148

answers:

6

Explanation: I don't remember about Linux and I don't know about OS X, but in Windows you can right-click a file and select a program to open it. But how does the OS know exactly how to make the program open it? Does it keep track of the "Open file" dialogs the program has? Does the developer have to specify a special event handler or something for these cases?

+1  A: 

The Windows Explorer remembers your previous "Open With..." choices based on file extension in the following key:

HKCR\.ext\OpenWithList

Next time you right-click the file, it looks there and build a list of programs you have previously used to open a particular file type.

Say it finds a key named "myapp.exe". It then looks up the application here:

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Applications\myapp.exe

fetches the info where the application is installed. And it goes here:

HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\myapp.exe

follows the GUID stored there to find out the display name of the application.

To add to the fun, the primary associated application is in the list as well, also everything in the OpenWithProgIds key and everything in:

HKEY_CLASSES_ROOT\*\OpenWithList

as well as their respective HKEY_CURRENT_USER counterparts.

The resulting list of applications is then made unique, sorted and displayed. On selection the file is started like any other file you click on - i.e.:

C:\path\to\myapp.exe "C:\path\to\the\file.ext"
Tomalak
extending your answer to solve this bit:"But how does the OS know exactly how to make the program open it? Does it keep track of the "Open file" dialogs the program has?" I would guess that it just passes the file name as the first parameter to the program, or uses ...I forget it's name...DDS?
Pod
A: 

http://windowsxp.mvps.org/OpenWith.htm

Pod
A: 

File associations are stored in the Registry in: HKEY_CLASSES_ROOT.

You can manage them graphically by using Windows Explorer: (WinXP)

Click Tools/Options/File Types

Or with the "Default Programs" applet in Control Panel. (Vista)

William Leara
File associations and "opens with..." are too slightly different things.
Pod
+3  A: 

For Windows, the answer is in the registry. If you're comfortable reading the registry, run regedit.exe on your Windows machine.

Under HKEY_CLASSES_ROOT, you will see a key list of all file types, .doc, .txt, etc. Each of these keys contains a key called "OpenWithList" or "OpenWithProgIds". An application may have a registered "ProgId", also found under HKEY_CLASSES_ROOT, and it can register it's ProgId to the file types it wants to handle in OpenWithProgIds. Otherwise it registers itself in OpenWithList.

In response to the comment to the first answer (cos I don't have enough rep to comment):

You're thinking of DDE, which is a near-enough deprecated technology. The Windows shell executes the application with the selected file as the first parameter.

Andy E
A: 

Just for some nostalgia the rather brilliant Acorn archimedes had a much better system with each file type having a unique type number, with a manufacturer and application code (rather like a MAC address) which was written with the file.
This means you can have different files all called .bak opened by the correct application - unlike the windows case where a new installed app steals the ownership of every exiting file of that type. Autocad is especialy bad for this, registering about 20 file types.

Martin Beckett
A: 

The operating system runs the specified program sending as parameter the path of the file to open.

For example, in C#, if you want to know which file the operating system wants you to open you'll need to do:

class Program
{
    static void Main(string[] args)
    {
        if (args.Length == 1) //The OS wants me to open a file
            openSomeFileJustBecauseTheOSWantsIt(args[0]);
    }
}
AlbertEin