Page 1 of 1

Open File With Associated Program (Windows)

Posted: Fri Feb 27, 2004 9:25 pm
by ebs
Code updated for 5.20+

Maybe this is old news, but I just learned this in my VB newsgroup.

To open any file with its associated program, all you need is one line!

For example, to show an HTML help file:

Code: Select all

ShellExecute_(GetDesktopWindow_(), "Open", "C:\windows\web\tip.htm", 0, 0, #SW_SHOWNORMAL)
To show a text file in Notepad:

Code: Select all

ShellExecute_(GetDesktopWindow_(), "Open", "C:\windows\system32\eula.txt", 0, 0, #SW_SHOWNORMAL)
I think that's pretty handy! :wink:

Posted: Fri Feb 27, 2004 9:36 pm
by AlGonzalez
You can also open a web url in the default browser:

Code: Select all

ShellExecute_(GetDesktopWindow_(), "Open", "http://www.purebasic.com", 0, 0, #SW_SHOWNORMAL)
Or bring up an email to send like this:

Code: Select all

ShellExecute_(GetDesktopWindow_(), "Open", "mailto:support@purebasic.com", 0, 0, #SW_SHOWNORMAL)
or with more information, like this:

Code: Select all

ShellExecute_(0, "Open", "mailto:support@purebasic.com?subject=cool&cc=dude@home.com&body=way cool", 0, 0, #SW_SHOWNORMAL)
BTW, while using GetDesktopWindow_() is technically the correct method, you can also just use 0.

Posted: Fri Feb 27, 2004 9:40 pm
by AlGonzalez
FYI - The above can also be accomplished using RunProgram:

Code: Select all

RunProgram("http://www.purebasic.com")

RunProgram("mailto:support@purebasic.com")

RunProgram("mailto:support@purebasic.com?subject=cool&cc=dude@home.com&body=way cool")

Posted: Fri Feb 27, 2004 10:05 pm
by merendo
But does anyone know how to associate a program with a filetype (the reverse way)?

Posted: Fri Feb 27, 2004 10:38 pm
by AlGonzalez
merendo wrote:But does anyone know how to associate a program with a filetype (the reverse way)?
One method, for Windows anyway, is to write to the registry under the HKEY_CLASSES_ROOT hive.
  1. create a key with the extension, for example ".xyz",
    with a default value of the key used to open your file extension
    (in this case "xyz.File").
  2. create a key to open your file extension, here we'll use "xyz.File\shell\open\command".
  3. Set the default for "xyz.File\shell\open" to the text you want displayed when the user
    right clicks on the file (It will default to "Open" if you don't set it)
  4. Set the default for "xyz.File\shell\open\command" to the command that will be executed
    for this file to open. i.e ("C:\Program Files\My XYZ App\xyz.exe" "%1")
    Make sure to use the quotes to make sure the app works with paths that have spaces.
    The %1 is replaced by the filename including the full path

Posted: Fri Feb 27, 2004 11:26 pm
by ebs
I should have known it!

I find what I think is a cool way to do something, only to discover that Fred is way ahead of me and already has it built into PureBasic!

Oh well... :wink:

Posted: Wed Mar 17, 2004 8:04 pm
by ricardo
I found this procedure here in the forum (i don't remember who code it :( ):

Code: Select all

Procedure SetKey(fold,Key$,Subkey$,Type,Adr,len) 
  If RegCreateKeyEx_(fold, Key$, 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, 0, @NewKey, @KeyInfo) = #ERROR_SUCCESS 
    RegSetValueEx_(NewKey, Subkey$, 0, Type,  Adr, len) 
    RegCloseKey_(NewKey) 
  EndIf 
EndProcedure 
Procedure AssociateFileEx(ext$,ext_description$,programm$,Icon$,prgkey$,cmd_description$,cmd_key$) 
  cmd$=Chr$(34)+programm$+Chr$(34)+" "+Chr$(34)+"%1"+Chr$(34) 
  If GetVersion_() & $FF0000  ; Windows NT/XP 
    SetKey(#HKEY_CLASSES_ROOT, "Applications\"+prgkey$+"\shell\"+cmd_description$+"\command","",#REG_SZ    ,@cmd$,Len(cmd$)+1) 
    If ext_description$ 
      Key$=ext$+"_auto_file" 
      SetKey(#HKEY_CLASSES_ROOT  ,"."+ext$           ,"",#REG_SZ,@Key$,Len(Key$)+1) 
      SetKey(#HKEY_CLASSES_ROOT  ,Key$               ,"",#REG_SZ,@ext_description$,Len(ext_description$)+1) 
      If Icon$ 
        SetKey(#HKEY_CLASSES_ROOT,Key$+"\DefaultIcon","",#REG_SZ,@Icon$,Len(Icon$)+1) 
      EndIf 
    EndIf 
    SetKey(#HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\."+ext$,"Application",#REG_SZ,@prgkey$         ,Len(prgkey$)+1) 
  Else ;Windows 9x 
    SetKey(#HKEY_LOCAL_MACHINE,"Software\Classes\."+ext$                        ,"",#REG_SZ,@prgkey$         ,Len(prgkey$)+1) 
    If ext_description$ 
      SetKey(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$                   ,"",#REG_SZ,@ext_description$,Len(ext_description$)+1) 
    EndIf 
    If Icon$ 
      SetKey(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$+"\DefaultIcon"    ,"",#REG_SZ,@Icon$           ,Len(Icon$)+1) 
    EndIf 
    If cmd_description$<>cmd_key$ 
      SetKey(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$+"\shell\"+cmd_key$,"",#REG_SZ,@cmd_description$,Len(cmd_description$)+1) 
    EndIf 
    SetKey(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$+"\shell\"+cmd_key$+"\command","",#REG_SZ,@cmd$   ,Len(cmd$)+1) 
  EndIf 
EndProcedure 
Procedure AssociateFile(ext$,ext_description$,programm$,Icon$) 
  AssociateFileEx(ext$,ext_description$,programm$,Icon$,GetFilePart(programm$),"open","open")  
EndProcedure 

;Usage
AssociateFile("ric","this is my extention", Programm$,"") 


Hope it helps!

Posted: Tue Mar 23, 2004 2:58 pm
by ebs
Does anyone know if (and how) "RunProgram()" can be used to open a directory?
For example, if I type "C:\Clients\" at the Windows "Run..." prompt in the Start Menu,
an Explorer window opens to that folder.
Is there a way to do the same thing with "RunProgram()" ?

Posted: Tue Mar 23, 2004 3:07 pm
by benny
@ebs:

Do you mean something like this :

Code: Select all

RunProgram("explorer.exe","c:\windows","")
:?: It opens an explorer in c:\windows ?!

Posted: Tue Mar 23, 2004 4:08 pm
by ebs
Benny,

Yes, that works.

I did some experimenting after I posted the message, and this works too:

Code: Select all

RunProgram("", "", "c:\windows") 
Thanks,
Eric