How to Identify the Default Application for a Specific File Type on Windows
When developing applications that interact with various file types, it becomes necessary to identify the default application associated with opening those files. To achieve this, Windows provides a method that leverages platform-specific functions.
The registry, while commonly used, is not a reliable means for retrieving this information, as demonstrated by instabilities in Windows 8.1. Therefore, the preferred approach is to utilize the Win32 API, particularly the AssocQueryString function.
Using AssocQueryString requires importing the necessary definitions from the Shlwapi.dll library and creating a wrapper method:
using System.Runtime.InteropServices; [DllImport("Shlwapi.dll", CharSet = CharSet.Unicode)] public static extern uint AssocQueryString( AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, ref uint pcchOut ); [Flags] public enum AssocF { None = 0, ... } public enum AssocStr { Command = 1, ... }
An example implementation can be found below:
static string AssocQueryString(AssocStr association, string extension) { ... return sb.ToString(); }
By calling this function with the appropriate parameters, developers can retrieve the path to the executable associated with opening a particular file type. This allows for seamless integration with external applications when launching files within your own software.
The above is the detailed content of How Can I Programmatically Determine the Default Application for a Specific File Type on Windows?. For more information, please follow other related articles on the PHP Chinese website!