473,563 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:
ProcessStartInf o pInfo = new ProcessStartInf o();
pInfo.FileName = @"calc.exe";
pInfo.UseShellE xecute = true;
Process p = Process.Start(p Info);

Mark
Jul 28 '08 #1
3 2539
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.co m

"LordHog" <Lo*****@gmail. comwrote in message
news:21******** *************** ***********@f63 g2000hsf.google groups.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:
ProcessStartInf o pInfo = new ProcessStartInf o();
pInfo.FileName = @"calc.exe";
pInfo.UseShellE xecute = true;
Process p = Process.Start(p Info);

Mark

Jul 28 '08 #2
On Jul 28, 10:56*am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.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.c om

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

news:21******** *************** ***********@f63 g2000hsf.google groups.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:
* *ProcessStartIn fo pInfo = new ProcessStartInf o();
* *pInfo.FileName = @"calc.exe";
* *pInfo.UseShell Execute = true;
* *Process p = Process.Start(p Info);
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.guar d.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.c om
"LordHog" <Lord...@gmail. comwrote in message
news:21******** *************** ***********@f63 g2000hsf.google groups.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:
* *ProcessStartIn fo pInfo = new ProcessStartInf o();
* *pInfo.FileName = @"calc.exe";
* *pInfo.UseShell Execute = true;
* *Process p = Process.Start(p Info);
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.guar d.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.c om
"LordHog" <Lord...@gmail. comwrote in message
news:21******** *************** ***********@f63 g2000hsf.google groups.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:
ProcessStartInf o pInfo = new ProcessStartInf o();
pInfo.FileName = @"calc.exe";
pInfo.UseShellE xecute = true;
Process p = Process.Start(p Info);
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
GetFileAssociat ionFull() which uses the GetFileAssociat ion() 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("shel l32", EntryPoint = "FindExecutable A",
CharSet = CharSet.Ansi, SetLastError = true,
ExactSpelling = true)]
private static extern int FindExecutable( string lpFile,
string lpDirectory,
StringBuilder Result);

private static readonly string
HKCU_APP_FULLNA ME = // HKEY_CURRENT_US ER
@"Software\Micr osoft\Windows\S hellNoRoam\MUIC ache";

const int MAX_PATH = 260,
ERROR_FILE_NO_A SSOCIATION = 31,
ERROR_FILE_NOT_ FOUND = 2,
ERROR_PATH_NOT_ FOUND = 3,
ERROR_FILE_SUCC ESS = 32,
ERROR_BAD_FORMA T = 11;
public static string GetFileAssociat ion(string fileName)
{
StringBuilder result = new StringBuilder(M AX_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.GetFileNam e(fileName),
Path.GetDirecto ryName(fileName ) + '\\', result);

switch (success)
{
case ERROR_FILE_NO_A SSOCIATION:
throw new InvalidOperatio nException("No association found");

case ERROR_FILE_NOT_ FOUND:
throw new FileNotFoundExc eption("File not found");

case ERROR_PATH_NOT_ FOUND:
throw new DirectoryNotFou ndException("Pa th not found");

case ERROR_BAD_FORMA T:
throw new InvalidOperatio nException("Bad format");

case ERROR_FILE_SUCC ESS:
break;
}

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

try
{
fileAssoc = GetFileAssociat ion( fileName );

if ( fileAssoc != null && !fileAssoc.Equa ls( string.Empty ) )
{
// Find out if a key named HKCU_APP_FULLNA ME exists in
// HKEY_CURRENT_US ER If it exists, open it
regKey = Registry.Curren tUser.OpenSubKe y( HKCU_APP_FULLNA ME );

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
2068
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 the mime type in IIS to be application/blobb My program is blobb.exe
18
2865
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 that applies to all these events, but I need to have specific code execute when the form closes. The properties for this method are sender (the...
0
3230
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 seems to indicate it can't find the <soap:body> element where it's looking for it. We are using the extension <mime:multipartRelated> to define some...
18
7699
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 programmatically using the common file dialog. Regardless, I need to determine the filetype/extension of each of these files already stored in my OLE...
8
1500
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 files or open openoffice if it is the default application for linux. is there a portable way to do this?
6
14309
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 be the answer) Thanks *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
3
1957
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 sending an window/console parameter from either app? Thanks, Dean Slindee
13
13193
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" HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=ABC.xls") HttpContext.Current.Response.Write(strHTML) HttpContext.Current.Response.End() However when the user tries to...
16
3232
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 do it. If I bring this same code outside this event handler, it works just fine. Is this normal?
0
7664
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7885
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7948
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.