473,327 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

how to open an application through an assigned filetype?

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.
Nov 16 '05 #1
6 2183
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

"Jesper." <an*******@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.com...
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.

Nov 16 '05 #2
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]
Nov 16 '05 #3
Morten Wennevik wrote:
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.

Nov 16 '05 #4
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]
Nov 16 '05 #5
Morten Wennevik wrote:
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.

Nov 16 '05 #6
Ah, thanks, it has been bugging me for some time :)

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jesper | last post by:
How can I open a textfile from C# using notepad (or the user assigned application for this).
7
by: kids_pro | last post by:
I found FileInfo expose alot of methods & properties. However I can't get FileType from FileInfo I can only get File extension. How can I get fileType any build-in library allow me to do that?...
1
by: Bon | last post by:
Hello all I create a form with three buttons in MS Access 2000. They are Open Excel Template, Save Draft and Save Final. When I click the Open Excel Template button, the Excel template will be...
2
by: Seok Bee | last post by:
Dear Experts, In my web application, I am having a button to open a file located in the server. When I click on the button to view the file, I received the following error message:...
3
by: Mark.V.Sullivan | last post by:
I have encountered the same problem another posted about several months ago. Unfortunately, there was no result posted on the old thread. I will let the original message text stand and ask if...
1
by: mekdam | last post by:
I've made an application and associated a filetype to it using the registry. When I double click the data file, it opens the application but doesn't load the data from the file, how do I fix this? ...
11
by: MLH | last post by:
The following procedures found at http://ffdba.com/downloads/Send_Mail_With_Outlook_Express.htm are meant to work together in harmony to effect eMail sends via OE. The last procedure (FN SplitB)...
0
by: colin.steadman | last post by:
If I right click on a ASP file in Explorer and choose EDIT, Windows opens a new instance of MSE.EXE and opens the file in that. What I'd like it to do is simply goto the existing open MSE...
3
by: MLH | last post by:
With Application.FileSearch .NewSearch .LookIn = "C:\My Documents" .SearchSubFolders = True .FileName = "Run" .MatchTextExactly = True .FileType = msoFileTypeAllFiles End With My code pukes...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.