Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
LordHog - 28 Jul 2008 18:46 GMT
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 Paldino [.NET/C# MVP] - 28 Jul 2008 18:54 GMT
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.

Signature

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

> Hello,
>
[quoted text clipped - 11 lines]
>
> Mark
LordHog - 28 Jul 2008 19:46 GMT
On Jul 28, 10:56 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
> Mark,
>
[quoted text clipped - 21 lines]
>
> > 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
LordHog - 31 Jul 2008 21:28 GMT
> On Jul 28, 10:56 am, "Nicholas Paldino [.NET/C# MVP]"
>
[quoted text clipped - 41 lines]
>
> Mark

> On Jul 28, 10:56 am, "Nicholas Paldino [.NET/C# MVP]"
>
[quoted text clipped - 41 lines]
>
> 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/Microsoft/Development/.NET/Visual_CSharp/Q_23604
651.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;
 }

}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.