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;
}
}