how to open an application through an assigned filetype?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Know of any good tutorials to demonstrate how to assign a file extension to an applicatiand and how to open the file in the application when you doubleclick on the file in the OS

brgds Jesper.
 
It's just a matter of adding some registry entries.
Translate this vb code to c#, should be pretty easy.
'// Registry windows api calls
Private Declare Function RegCreateKey& Lib "advapi32.DLL" Alias
"RegCreateKeyA" (ByVal hKey As Long, ByVal lpszSubKey As String, lphKey As
Long)
Private Declare Function RegSetValue& Lib "advapi32.DLL" Alias
"RegSetValueA" (ByVal hKey As Long, ByVal lpszSubKey As String, ByVal
fdwType As Long, ByVal lpszValue As String, ByVal dwLength As Long)
'// Required constants
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const MAX_PATH = 256&
Private Const REG_SZ = 1

'// procedure you call to associate the tmg extension with your program.
Private Sub MakeDefault()
Dim sKeyName As String '// Holds Key Name in registry.
Dim sKeyValue As String '// Holds Key Value in registry.
Dim ret As Long '// Holds error status if any from API calls.
Dim lphKey As Long '// Holds created key handle from RegCreateKey.

'// This creates a Root entry called "TextMagic"
sKeyName = "TextMagic" '// Application Name
sKeyValue = "TextMagic Document" '// File Description
ret = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey)
ret = RegSetValue&(lphKey&, Empty, REG_SZ, sKeyValue, 0&)

'// This creates a Root entry called .tmg associated with "TextMagic".
sKeyName = ".tmg" '// File Extension
sKeyValue = "TextMagic" '// Application Name
ret = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey)

ret = RegSetValue&(lphKey, Empty, REG_SZ, sKeyValue, 0&)

'//This sets the command line for "TextMagic".
sKeyName = "TextMagic" '// Application Name
If Right$(App.Path,1) = "\" Then
sKeyValue = App.Path & App.EXEName & ".exe %1" '// Application Path
Else
sKeyValue = App.Path & "\" & App.EXEName & ".exe %1" '// Application
Path
End If
ret = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey)
ret = RegSetValue&(lphKey, "shell\open\command", REG_SZ, sKeyValue,
MAX_PATH)
End Sub

Private Sub Form_Load()
'// ensure we only register once. When debugging etc, remove the
SaveSetting line, so your program will
'// always attempt to register the file extension
If GetSetting(App.Title, "Settings", "RegisteredFile", 0) = 0 Then
'// associate tmg extension with this app
MakeDefault
SaveSetting App.Title, "Settings", "RegisteredFile", 1
End If
End Sub
 
Hi Jesper,

Not sure how to assign file type, but what happens when you click on a
"known" file is that the program is launched with the name of the file as
the first argument in the argument list.

public static void Main(string[] args)
{
if(args.Length > 0)
Application.Run(new Form1(args[0]));
else
Application.Run(new Form1());
}

Happy coding!
Morten Wennevik [C# MVP]
 
Morten said:
Hi Jesper,

Not sure how to assign file type, but what happens when you click on a
"known" file is that the program is launched with the name of the
file as the first argument in the argument list.

This isn't necessarily true, although it is a typical behavior. When a file
assocation is created there are a set of registry keys that get added to the
registry. These keys contain the information used by the Windows OS as to
what to do when a file of that type is executed. This includes command
information. Often the command info may simply be the name of some
application followed by a %1, instructing Windows to pass the file that was
double clicked as the command argument. However there can certainly be
other arguments listed in the file association as well that may get passed
on the command line. The application may also use DDE, in which case
typically you will not see the file name passed on the command line at all.
--
Tom Porterfield
MS-MVP MCE
http://support.telop.org

Please post all follow-ups to the newsgroup only.
 
Yeah, I remember seeing something like this in the registry.

I know Internet Explorer reacts differently to Process.Start and passing
the url directly to iexplore.exe compared to passing the url directly to
windows (the latter overwrites an already existing IE window, while the
former opens a new window). Not sure why though.

Happy coding!
Morten Wennevik [C# MVP]
 
Morten said:
Yeah, I remember seeing something like this in the registry.

I know Internet Explorer reacts differently to Process.Start and
passing the url directly to iexplore.exe compared to passing the url
directly to windows (the latter overwrites an already existing IE
window, while the former opens a new window). Not sure why though.

That's an IE setting (although I guess we're getting offtopic here since
we're now into the workings of windows and IE rather than coding). In the
IE advanced settings under browsing you can select or unselect the option to
reuse browser windows. So in the former you are specifically invoking an
iexplore.exe process as an executable with command line, in the latter you
are telling windows to figure out what to do with the command.
--
Tom Porterfield
MS-MVP MCE
http://support.telop.org

Please post all follow-ups to the newsgroup only.
 
Ah, thanks, it has been bugging me for some time :)

Happy coding!
Morten Wennevik [C# MVP]
 
Back
Top