473,224 Members | 1,663 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,224 software developers and data experts.

Determine the default mime handler for a file type then launch thathandler

Hello,

How would I go about finding the default handler, let's say a text
file (*.txt), then launch the default handler with the file as an
argument? I had found how to launch an external program, but I do not
know how I would find the default handler to a file type. Any help is
greatly appreciated.

Code to launch application:
ProcessStartInfo pInfo = new ProcessStartInfo();
pInfo.FileName = @"calc.exe";
pInfo.UseShellExecute = true;
Process p = Process.Start(pInfo);

Mark
Jul 28 '08 #1
3 2520
Mark,

Instead of using the application that launches it, just use the path to
the file itself. The OS will take care of the rest and launch the
appropriate application.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"LordHog" <Lo*****@gmail.comwrote in message
news:21**********************************@f63g2000 hsf.googlegroups.com...
Hello,

How would I go about finding the default handler, let's say a text
file (*.txt), then launch the default handler with the file as an
argument? I had found how to launch an external program, but I do not
know how I would find the default handler to a file type. Any help is
greatly appreciated.

Code to launch application:
ProcessStartInfo pInfo = new ProcessStartInfo();
pInfo.FileName = @"calc.exe";
pInfo.UseShellExecute = true;
Process p = Process.Start(pInfo);

Mark

Jul 28 '08 #2
On Jul 28, 10:56*am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Mark,

* * Instead of using the application that launches it, just use the path to
the file itself. *The OS will take care of the rest and launch the
appropriate application.

--
* * * * * - Nicholas Paldino [.NET/C# MVP]
* * * * * - m...@spam.guard.caspershouse.com

"LordHog" <Lord...@gmail.comwrote in message

news:21**********************************@f63g2000 hsf.googlegroups.com...
Hello,
*How would I go about finding the default handler, let's say a text
file (*.txt), then launch the default handler with the file as an
argument? I had found how to launch an external program, but I do not
know how I would find the default handler to a file type. Any help is
greatly appreciated.
Code to launch application:
* *ProcessStartInfo pInfo = new ProcessStartInfo();
* *pInfo.FileName = @"calc.exe";
* *pInfo.UseShellExecute = true;
* *Process p = Process.Start(pInfo);
Mark
Nicholas,

Thanks for the quick reply, but the other aspect of what I am trying
to do is have a context menu that will say (dynamically)

"Launch *.txt in Notepad" or "Launch *.txt in UltraEdit" or ...
whatever the default handler for the mime type is

Sorry I did not mention this before, but just another piece of the
puzzle.

Mark
Jul 28 '08 #3
On Jul 28, 11:46*am, LordHog <Lord...@gmail.comwrote:
On Jul 28, 10:56*am, "Nicholas Paldino [.NET/C# MVP]"

<m...@spam.guard.caspershouse.comwrote:
Mark,
* * Instead of using the application that launches it, just use thepath to
the file itself. *The OS will take care of the rest and launch the
appropriate application.
--
* * * * * - Nicholas Paldino [.NET/C# MVP]
* * * * * - m...@spam.guard.caspershouse.com
"LordHog" <Lord...@gmail.comwrote in message
news:21**********************************@f63g2000 hsf.googlegroups.com....
Hello,
*How would I go about finding the default handler, let's say a text
file (*.txt), then launch the default handler with the file as an
argument? I had found how to launch an external program, but I do not
know how I would find the default handler to a file type. Any help is
greatly appreciated.
Code to launch application:
* *ProcessStartInfo pInfo = new ProcessStartInfo();
* *pInfo.FileName = @"calc.exe";
* *pInfo.UseShellExecute = true;
* *Process p = Process.Start(pInfo);
Mark

Nicholas,

* Thanks for the quick reply, but the other aspect of what I am trying
to do is have a context menu that will say (dynamically)

* "Launch *.txt in Notepad" or "Launch *.txt in UltraEdit" or ...
whatever the default handler for the mime type is

* Sorry I did not mention this before, but just another piece of the
puzzle.

Mark


On Jul 28, 11:46 am, LordHog <Lord...@gmail.comwrote:
On Jul 28, 10:56 am, "Nicholas Paldino [.NET/C# MVP]"

<m...@spam.guard.caspershouse.comwrote:
Mark,
Instead of using the application that launches it, just use the path to
the file itself. The OS will take care of the rest and launch the
appropriate application.
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
"LordHog" <Lord...@gmail.comwrote in message
news:21**********************************@f63g2000 hsf.googlegroups.com....
Hello,
How would I go about finding the default handler, let's say a text
file (*.txt), then launch the default handler with the file as an
argument? I had found how to launch an external program, but I do not
know how I would find the default handler to a file type. Any help is
greatly appreciated.
Code to launch application:
ProcessStartInfo pInfo = new ProcessStartInfo();
pInfo.FileName = @"calc.exe";
pInfo.UseShellExecute = true;
Process p = Process.Start(pInfo);
Mark

Nicholas,

Thanks for the quick reply, but the other aspect of what I am trying
to do is have a context menu that will say (dynamically)

"Launch *.txt in Notepad" or "Launch *.txt in UltraEdit" or ...
whatever the default handler for the mime type is

Sorry I did not mention this before, but just another piece of the
puzzle.

Mark

Hello all,

I did get a solution from Experts-Exchange and I had modified the
answer just slightly to obtain the results I was looking for. The
original thread can be found here.

http://www.experts-exchange.com/Micr...html#a22122165

It appears that the FindExecutable() is the classic solution and
there are a lot of postings of it. I did add the interface
GetFileAssociationFull() which uses the GetFileAssociation() interface
in conjunction with the registry to pull the full names just as the
"Open With" functionality of Windows Explorer does, or at least from
what I can tell. I have read else where that the function
FindExecutable() will fail if the extension is longer than 3
characters. I have not tried it out, but just a fair warning.

Mark


using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;

sealed public class FileAssociation
{
[DllImport("shell32", EntryPoint = "FindExecutableA",
CharSet = CharSet.Ansi, SetLastError = true,
ExactSpelling = true)]
private static extern int FindExecutable(string lpFile,
string lpDirectory,
StringBuilder Result);

private static readonly string
HKCU_APP_FULLNAME = // HKEY_CURRENT_USER
@"Software\Microsoft\Windows\ShellNoRoam\MUICache" ;

const int MAX_PATH = 260,
ERROR_FILE_NO_ASSOCIATION = 31,
ERROR_FILE_NOT_FOUND = 2,
ERROR_PATH_NOT_FOUND = 3,
ERROR_FILE_SUCCESS = 32,
ERROR_BAD_FORMAT = 11;
public static string GetFileAssociation(string fileName)
{
StringBuilder result = new StringBuilder(MAX_PATH);

//lpFile: name of the file of interest
//lpDirectory: location of lpFile
//sResult: path and name of executable associated with lpFile
int success =
FindExecutable(Path.GetFileName(fileName),
Path.GetDirectoryName(fileName) + '\\', result);

switch (success)
{
case ERROR_FILE_NO_ASSOCIATION:
throw new InvalidOperationException("No association found");

case ERROR_FILE_NOT_FOUND:
throw new FileNotFoundException("File not found");

case ERROR_PATH_NOT_FOUND:
throw new DirectoryNotFoundException("Path not found");

case ERROR_BAD_FORMAT:
throw new InvalidOperationException("Bad format");

case ERROR_FILE_SUCCESS:
break;
}

return result.ToString();
}
public static string GetFileAssociationFull(string fileName)
{
string fileAssoc;
RegistryKey regKey;

try
{
fileAssoc = GetFileAssociation( fileName );

if ( fileAssoc != null && !fileAssoc.Equals( string.Empty ) )
{
// Find out if a key named HKCU_APP_FULLNAME exists in
// HKEY_CURRENT_USER If it exists, open it
regKey = Registry.CurrentUser.OpenSubKey( HKCU_APP_FULLNAME );

if ( regKey != null )
{
// Read the Full Application Name, if present
if ( regKey.GetValue( fileAssoc ) != null )
{
fileAssoc = regKey.GetValue( fileAssoc ).ToString();
}

// Release the resources that the registry variable was
using
regKey.Close();
}
}
}
catch( Exception e )
{
fileAssoc = string.Empty;
}

return fileAssoc;
}

}
Jul 31 '08 #4

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

Similar topics

1
by: Mike Curry | last post by:
I am trying to create a program that will launch when my .blobb file gets downloaded from a web page. How do I associate my files .blobb extension with my Visual Basic application? I added...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
0
by: clevariant | last post by:
Hello, I'm tasked with creating a WSDL file that is compatible with .NET's code gen' tool, wsdl.exe. I'm getting an error in the code output saying "Missing soap:body input binding", which...
18
by: Keith Brown | last post by:
I have an application that allows embedded storage of ANY chosen file in an OLE field. The file could have been dragged-and-dropped into the field or it might have been selected and imported...
8
by: Jerry Gaspard | last post by:
I'd like to know how to open a document in a default app. like for example if I wanted to open a .txt document I'd like my program open notepad if it is the default for a windows machine text...
6
by: Kaki | last post by:
Given a file, how do I know if it's ascii or unicode or binary? And how do I know if it's rtf or html or etc? In other words, how do I find the stream type or mime type? (No, file extension cannot...
3
by: Dean Slindee | last post by:
I have a exception handling class that could be called from either a windows project app or a console project app. Is there any way for this class to determine which type of app called it without...
13
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"...
16
by: Peter Oliphant | last post by:
Note that although this involves SAPI, it is more a question about Timers and event handlers. I wrote a Speech Recognize handler (SAPI), and put some code in it to enable a Timer. It would not...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.