|
-
Mar 20th, 2003, 03:29 AM
#1
Thread Starter
New Member
Checking if file handler is installed
Hi
I'm wondering if there is any way to determine if there is a program installed that can handle a given file type and they use it to open a given document.
E.g I want to be able to execute a PDF or a DXW file from within my vbNet application but only if a PDF or DXW viewer is installed and registiered otherwise I want to trap the fact that a handler isn't installed and bring up a message box.
Thanks,
Sarah
-
Mar 21st, 2003, 10:58 AM
#2
Thread Starter
New Member
[RESOLVED]
Just in case someone else has a similar problem in the future.
I found some old code on PlanetSourcecode and updated it to work with VB.NET. I also added a new error message in the appropriate place with regard to file associations.
Sarah
'**************************************
'Windows API/Global Declarations for :Execute_Program
'**************************************
'Declares for Execute_Program
Declare Sub BringWindowToTop Lib "user" (ByVal hWnd As Integer)
Declare Function SendMessage Lib "User" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Long
Const WM_CLOSE = &H10
Declare Sub SetWindowPos Lib "user" (ByVal hWnd%, ByVal hwndAfter%, ByVal x%, ByVal y%, ByVal cx%, ByVal cy%, ByVal swp%)
Const HWND_TOP = 0
Const HWND_BOTTOM = 1
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const GWL_ID = (-12)
Const GW_HWNDNEXT = 2
Const GW_CHILD = 5
Const FWP_STARTSWITH = 0
Const FWP_CONTAINS = 1
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Integer, ByVal lpszOp As String, ByVal lpszFile As String, ByVal spszParams As String, ByVal lpszDir As String, ByVal fsShowCmd As Integer) As Integer
Const SW_SHOW = 5
'**************************************
' Name: Execute_Program
' Description:Runs a program and handles all possible errors (such as running out
' of memory, file can't be opened, etc.) Also, unlike the VB Shell command, it allows
' you to specify a 'default working directory'!
' Also, allows you to run a file that is only an association--example:you can run a .txt file with this function!
' By: Ian Ippolito (RAC)
'
' Minor upadates to allow integration with 32 bit environments and to allow error message "No association for this file."
' By Sarah Connor
'
' Inputs:strFilePath--program to run
' strParms--program command line parms (if any)
' strDir--default working directory
'
' Returns:returns TRUE=successful
' FALSE=failed
'
' Assumes:None
'
' Side Effects:None
' This code is copyrighted and has limited warranties.
' Please see http://www.Planet-Source-Code.com/xq...s/ShowCode.htm for details.
'**************************************
Function Execute_Program(ByVal strFilePath As String, ByVal strParms As String, ByVal strDir As String) As Integer
'run program
Dim hwndProgram As Integer
hwndProgram = ShellExecute(0, "Open", strFilePath, strParms, strDir, SW_SHOW)
'evaluate errors
Select Case (hwndProgram)
Case 0
MsgBox("Insufficent system memory or corrupt program file.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 2
MsgBox("File not found.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 3
MsgBox("Invalid path.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 5
MsgBox("Sharing or Protection Error.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 6
MsgBox("Seperate data segments are required for each task.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 8
MsgBox("Insufficient memory to run the program.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 10
MsgBox("Incorrect Windows version.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 11
MsgBox("Invalid program file.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 12
MsgBox("Program file requires a different operating system.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 13
MsgBox("Program requires MS-DOS 4.0.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 14
MsgBox("Unknown program file type.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 15
MsgBox("Windows program does not support protected memory mode.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 16
MsgBox("Invalid use of data segments when loading a second instance of a program.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 19
MsgBox("Attempt to run a compressed program file.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 20
MsgBox("Invalid dynamic link library.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 21
MsgBox("Program requires Windows 32-bit extensions.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 31
MsgBox("No association for this file", vbOKOnly + vbInformation, strFilePath)
Exit Function
End Select
Execute_Program = True
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' To open an INI file with the associated file handler (generally NOTEPAD.EXE) use:
Execute_Program("c:\windows\win.ini", "", "")
' To open an JPG image with the associated file handler use:
Execute_Program("c:\mypic.jpg", "", "")
End Sub
-
Mar 21st, 2003, 12:35 PM
#3
Addicted Member
Just in case someone is interested in reading it.
VB Code:
'**************************************
'Windows API/Global Declarations for :Execute_Program
'**************************************
'Declares for Execute_Program
Declare Sub BringWindowToTop Lib "user" (ByVal hWnd As Integer)
Declare Function SendMessage Lib "User" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Long
Const WM_CLOSE = &H10
Declare Sub SetWindowPos Lib "user" (ByVal hWnd%, ByVal hwndAfter%, ByVal x%, ByVal y%, ByVal cx%, ByVal cy%, ByVal swp%)
Const HWND_TOP = 0
Const HWND_BOTTOM = 1
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const GWL_ID = (-12)
Const GW_HWNDNEXT = 2
Const GW_CHILD = 5
Const FWP_STARTSWITH = 0
Const FWP_CONTAINS = 1
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Integer, ByVal lpszOp As String, ByVal lpszFile As String, ByVal spszParams As String, ByVal lpszDir As String, ByVal fsShowCmd As Integer) As Integer
Const SW_SHOW = 5
'**************************************
' Name: Execute_Program
' Description:Runs a program and handles all possible errors (such as running out
' of memory, file can't be opened, etc.) Also, unlike the VB Shell command, it allows
' you to specify a 'default working directory'!
' Also, allows you to run a file that is only an association--example:you can run a .txt file with this function!
' By: Ian Ippolito (RAC)
'
' Minor upadates to allow integration with 32 bit environments and to allow error message "No association for this file."
' By Sarah Connor
'
' Inputs:strFilePath--program to run
' strParms--program command line parms (if any)
' strDir--default working directory
'
' Returns:returns TRUE=successful
' FALSE=failed
'
' Assumes:None
'
' Side Effects:None
' This code is copyrighted and has limited warranties.
' Please see [url]http://www.Planet-Source-Code.com/x...ts/ShowCode.htm[/url] for details.
'**************************************
Function Execute_Program(ByVal strFilePath As String, ByVal strParms As String, ByVal strDir As String) As Integer
'run program
Dim hwndProgram As Integer
hwndProgram = ShellExecute(0, "Open", strFilePath, strParms, strDir, SW_SHOW)
'evaluate errors
Select Case (hwndProgram)
Case 0
MsgBox("Insufficent system memory or corrupt program file.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 2
MsgBox("File not found.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 3
MsgBox("Invalid path.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 5
MsgBox("Sharing or Protection Error.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 6
MsgBox("Seperate data segments are required for each task.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 8
MsgBox("Insufficient memory to run the program.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 10
MsgBox("Incorrect Windows version.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 11
MsgBox("Invalid program file.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 12
MsgBox("Program file requires a different operating system.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 13
MsgBox("Program requires MS-DOS 4.0.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 14
MsgBox("Unknown program file type.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 15
MsgBox("Windows program does not support protected memory mode.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 16
MsgBox("Invalid use of data segments when loading a second instance of a program.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 19
MsgBox("Attempt to run a compressed program file.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 20
MsgBox("Invalid dynamic link library.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 21
MsgBox("Program requires Windows 32-bit extensions.", 0, "Error running " & strFilePath)
Execute_Program = False
Exit Function
Case 31
MsgBox("No association for this file", vbOKOnly + vbInformation, strFilePath)
Exit Function
End Select
Execute_Program = True
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' To open an INI file with the associated file handler (generally NOTEPAD.EXE) use:
Execute_Program("c:\windows\win.ini", "", "")
' To open an JPG image with the associated file handler use:
Execute_Program("c:\mypic.jpg", "", "")
End Sub
-
Mar 21st, 2003, 12:52 PM
#4
Sleep mode
Is there any .NET way ?? this looks messy at least for me
-
Mar 21st, 2003, 12:57 PM
#5
Thread Starter
New Member
Appologies for not posting it in colour, it was my first post with code in it. Do you just set the HTML toggle to on to do that?
Yes it is rather messy, I was rather hoping someone would offer a more elegant solution. I've only been using vb.net for 2 weeks and I'm still trying to get my head around the whole thing.
Sarah
-
Mar 21st, 2003, 01:00 PM
#6
Sleep mode
-
Mar 21st, 2003, 01:03 PM
#7
Sleep mode
hmmm didn't show the formula I sent !
click code button , then type vb between " [Highlight=VB] and this [/vbcode ] "
hope it works this time !
-
Mar 21st, 2003, 02:00 PM
#8
Frenzied Member
Dont you think that searching the registry for file type association and then finding that program and initializing a new process of that viewer will be a very easy and neat .NET way of this?
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Mar 21st, 2003, 02:18 PM
#9
Frenzied Member
To be more accurate imagine you want to find what program is the viewer of pdf files on the target computer
1. In the HKEY_CLASSES_ROOT of the registry you will find a key called .jpg
2. That key has a String (REG_SZ) value called (deafult) that shows the file type, in this case for example ACDSee.JPG
3. Again in HKEY_CLASSES_ROOT will be another key called ACDSee.JPG (the value from (defalut) entry of previous key)
4. If you dig this key you will find:shell, open and command:
HKEY_CLASSES_ROOT\ACDSee.JPG\shell\Open\Command
5. Again the (default) value of command Key is the path to the file that opens this kind of file.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Mar 21st, 2003, 06:28 PM
#10
Thread Starter
New Member
Lunatic, to be honest that doesn't seem like a nicer or easier way of doing things. My (and I use that loosly) way calls the associated app through a system API call with built-in error handling. It also lets you use one small-ish piece of code to execute any registered file type.
If I'm reading your idea correctly, and I'm pretty new at VB.net so forgive me if I'm not, you suggest checking the registry for every file type you want to execute and then manually traping the potential errors. That sounds like a lot of un-necesary programming when the wheel has already been invented.
Just my 0.02c
Sarah
-
Mar 22nd, 2003, 02:20 AM
#11
Frenzied Member
You may be right, but all you need to do is writing few lines of pure .NET code. I thought generally it would be a good idea to avoid non native .NET code, although its not possible all the time.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Mar 22nd, 2003, 08:44 AM
#12
Thread Starter
New Member
Originally posted by Lunatic3
You may be right, but all you need to do is writing few lines of pure .NET code. I thought generally it would be a good idea to avoid non native .NET code, although its not possible all the time.
Hi Lunatic3
I'd be thrilled to see an example in pure .NET code, can you show me how this is achieved with some example code.
Sarah
-
Mar 22nd, 2003, 10:57 AM
#13
Frenzied Member
Sarah
If you just want to open a file with its default viewer then this will be easy i guess, you can let windows handle that. That registry stuff was about to check what will open a given file type.
VB Code:
Dim p As New Process(), s As String
Try
s = "C:\myfile.pdf"
p.Start(s)
Catch ex As System.Exception
MessageBox.Show(ex.Message)
End Try
If u still want the code about reading the registry i will post it.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Mar 22nd, 2003, 01:04 PM
#14
Thread Starter
New Member
Originally posted by Lunatic3
Sarah
If you just want to open a file with its default viewer then this will be easy i guess, you can let windows handle that. That registry stuff was about to check what will open a given file type.
VB Code:
Dim p As New Process(), s As String
Try
s = "C:\myfile.pdf"
p.Start(s)
Catch ex As System.Exception
MessageBox.Show(ex.Message)
End Try
If u still want the code about reading the registry i will post it.
This is the answer to the original question I asked! Oh well, I guess I learned something about API calls at least.
The registry code may well come in usuful, I'm here to learn after all. Please post it.
Sarah
-
Mar 23rd, 2003, 06:48 AM
#15
Frenzied Member
Sarah
Here is the code to read the registry for the file that opens the specified extension.
VB Code:
Imports Microsoft.Win32
'
'
'
'
'
Dim rgkey As RegistryKey
Dim ext As String = ".pdf"
Try
rgkey = Registry.ClassesRoot.OpenSubKey(ext)
If rgkey Is Nothing Then
MessageBox.Show("The file extention is unknown!")
Exit Sub
End If
ext = rgkey.GetValue("")
rgkey = Registry.ClassesRoot.OpenSubKey(ext).OpenSubKey("shell").OpenSubKey("open").OpenSubKey("command")
If rgkey Is Nothing Then
MessageBox.Show("No 'Open' event is associated with this extension!")
Exit Sub
End If
ext = rgkey.GetValue("")
ext = ext.Split(Chr(34))(1)
MessageBox.Show(ext)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Mar 23rd, 2003, 06:58 AM
#16
Thread Starter
New Member
That's a great piece if code Lunatic3. I'll be sure to place it in my reference library as I'm bound to have a need for registry look-up stuff in the future.
Thank you for sharing.
Sarah
-
Mar 23rd, 2003, 08:41 AM
#17
Sleep mode
.NET Framework has a class named Registery.It's more powerful and efficient than those APIs . It contains Security stuff that existed in XP , WIN2000 , and .NET OS .So if you use it , it should be forward compatible.
-
Mar 23rd, 2003, 10:15 AM
#18
Addicted Member
A class more powerful than an API? A bit hard to believe...
-
Mar 23rd, 2003, 11:57 AM
#19
PowerPoster
Its not more powerful, but wraps up all the api's into easy to use classes.
-
Mar 23rd, 2003, 12:15 PM
#20
Sleep mode
Originally posted by Zealot
A class more powerful than an API? A bit hard to believe...
at least , it might be in this case and it's .NET way .These classes are the replacement of those APIs .
-
Oct 25th, 2004, 06:35 PM
#21
Addicted Member
I've run into a similar problem and was wondering, is there a way to invoke the Windows unknown file dialog? ie. the one that appears whenever you double click on a file with an extension it doesn't know how to handle.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|