finding_20the_20application_20associated_20with_20an_20extension

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
finding_20the_20application_20associated_20with_20an_20extension [2018/03/31 13:19] – external edit 127.0.0.1finding_20the_20application_20associated_20with_20an_20extension [2024/01/05 00:22] (current) – external edit 127.0.0.1
Line 2: Line 2:
  
 //by J.G.Harston, August 2008//\\ \\  The application associated with a particular file extension (actually, the command used to launch or open a file) is stored in the system registry. This associated command can be found with the following code:\\  //by J.G.Harston, August 2008//\\ \\  The application associated with a particular file extension (actually, the command used to launch or open a file) is stored in the system registry. This associated command can be found with the following code:\\ 
 +<code bb4w>
         DEF FNFile_Association(ext$)         DEF FNFile_Association(ext$)
         LOCAL root$,command$,A%         LOCAL root$,command$,A%
Line 10: Line 11:
         A%=INSTR(command$,""""""):IF A% THEN command$=LEFT$(command$,A%-1)+MID$(command$,A%+2)         A%=INSTR(command$,""""""):IF A% THEN command$=LEFT$(command$,A%-1)+MID$(command$,A%+2)
         =FNPath_Expand(command$)         =FNPath_Expand(command$)
 +</code>
 So, for example, **FNFile_Association(".txt")** may return **"C:\Program Files\Metapad\metapad.exe"**. If there is no file association, then a null string is returned.\\ \\  The above code uses the following code from the [[http://mdfs.net/blib|Registry]] library and a routine called **FNPath_Expand**:\\  So, for example, **FNFile_Association(".txt")** may return **"C:\Program Files\Metapad\metapad.exe"**. If there is no file association, then a null string is returned.\\ \\  The above code uses the following code from the [[http://mdfs.net/blib|Registry]] library and a routine called **FNPath_Expand**:\\ 
 +<code bb4w>
         DEF FNRegistry_RdInt(hk%,Key$,Item$):LOCAL Want%:Want%=4         DEF FNRegistry_RdInt(hk%,Key$,Item$):LOCAL Want%:Want%=4
         DEF FNRegistry_RdBig(hk%,Key$,Item$):LOCAL Want%:Want%=4         DEF FNRegistry_RdBig(hk%,Key$,Item$):LOCAL Want%:Want%=4
Line 33: Line 36:
         SYS "ExpandEnvironmentStrings", S$, B%, 256         SYS "ExpandEnvironmentStrings", S$, B%, 256
         =$$B%         =$$B%
 +</code>
 \\ \\
 ---- ----
 //by Richard Russell, September 2008//\\ \\  If you prefer not to access the Registry directly, the function below will return the path and name of the executable program which is associated with a particular file:\\ \\  //by Richard Russell, September 2008//\\ \\  If you prefer not to access the Registry directly, the function below will return the path and name of the executable program which is associated with a particular file:\\ \\ 
 +<code bb4w>
         DEF FN_FindExecutable(file$)         DEF FN_FindExecutable(file$)
         LOCAL executable{}         LOCAL executable{}
Line 41: Line 46:
         SYS "FindExecutable", file$, "", executable{}         SYS "FindExecutable", file$, "", executable{}
         = executable.name&()         = executable.name&()
 +</code>
 The main difference from the code listed by Jonathan Harston is that rather than supplying just an extension you must supply the name of an actual file with that extension, which must exist. To overcome that restriction the following function temporarily creates such a file, and in so doing reproduces the functionality of DEFFNFile_Association:\\ \\  The main difference from the code listed by Jonathan Harston is that rather than supplying just an extension you must supply the name of an actual file with that extension, which must exist. To overcome that restriction the following function temporarily creates such a file, and in so doing reproduces the functionality of DEFFNFile_Association:\\ \\ 
 +<code bb4w>
         DEF FNFile_Association(ext$)         DEF FNFile_Association(ext$)
         LOCAL executable{}         LOCAL executable{}
Line 49: Line 56:
         OSCLI "DELETE temp"+ext$         OSCLI "DELETE temp"+ext$
         = executable.name&()         = executable.name&()
 +</code>
 Note that this function relies on the 'current directory' being writable.\\ \\ Note that this function relies on the 'current directory' being writable.\\ \\
 ---- ----
 //by J.G.Harston, May 2009//\\ \\  BBC BASIC for Windows version 5.90 introduced the @tmp$ system variable which points to a writable temporary directory. The above code can be rewritten to avoid relying on writing to the current directory:\\ \\  //by J.G.Harston, May 2009//\\ \\  BBC BASIC for Windows version 5.90 introduced the @tmp$ system variable which points to a writable temporary directory. The above code can be rewritten to avoid relying on writing to the current directory:\\ \\ 
 +<code bb4w>
         DEF FNFile_Association(ext$)         DEF FNFile_Association(ext$)
         LOCAL executable{}         LOCAL executable{}
Line 59: Line 68:
         OSCLI "DELETE """+@tmp$+"temp"+ext$+""""         OSCLI "DELETE """+@tmp$+"temp"+ext$+""""
         = executable.name&()         = executable.name&()
 +</code>
finding_20the_20application_20associated_20with_20an_20extension.1522502360.txt.gz · Last modified: 2024/01/05 00:17 (external edit)