Open File With Associated Program (Windows)

Share your advanced PureBasic knowledge/code with the community.
ebs
Enthusiast
Enthusiast
Posts: 557
Joined: Fri Apr 25, 2003 11:08 pm

Open File With Associated Program (Windows)

Post 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:
AlGonzalez
User
User
Posts: 53
Joined: Sun Feb 15, 2004 6:04 am
Location: Easley, SC, USA

Post 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.
Last edited by AlGonzalez on Fri Feb 27, 2004 9:41 pm, edited 1 time in total.
AlGonzalez
User
User
Posts: 53
Joined: Sun Feb 15, 2004 6:04 am
Location: Easley, SC, USA

Post 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")
merendo
Enthusiast
Enthusiast
Posts: 449
Joined: Sat Apr 26, 2003 7:24 pm
Location: Germany
Contact:

Post by merendo »

But does anyone know how to associate a program with a filetype (the reverse way)?
The truth is never confined to a single number - especially scientific truth!
AlGonzalez
User
User
Posts: 53
Joined: Sun Feb 15, 2004 6:04 am
Location: Easley, SC, USA

Post 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
ebs
Enthusiast
Enthusiast
Posts: 557
Joined: Fri Apr 25, 2003 11:08 pm

Post 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:
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post 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!
ARGENTINA WORLD CHAMPION
ebs
Enthusiast
Enthusiast
Posts: 557
Joined: Fri Apr 25, 2003 11:08 pm

Post 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()" ?
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

@ebs:

Do you mean something like this :

Code: Select all

RunProgram("explorer.exe","c:\windows","")
:?: It opens an explorer in c:\windows ?!
regards,
benny!
-
pe0ple ar3 str4nge!!!
ebs
Enthusiast
Enthusiast
Posts: 557
Joined: Fri Apr 25, 2003 11:08 pm

Post 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
Post Reply