private static extern HRESULT AssocQueryString( AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, ref uint pcchOut);
internal static extern uint AssocQueryString( AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, [In][Out] ref uint pcchOut);
/* * * AssocStr str * File name extension * A file name extension, such as .txt. * * CLSID * A CLSID GUID in the standard "{GUID}" format. * * ProgID * An application's ProgID, such as Word.Document.8. * * Executable name * The name of an application's .exe file. The ASSOCF_OPEN_BYEXENAME flag must be set in flags. */ private static string GetAssociation(AssocF assocF, AssocStr assocStr, string doctype, string pszExtra = null) { try { uint pcchOut = 0; // size of output buffer // First call is to get the required size of output buffer AssocQueryString(assocF, assocStr, doctype, pszExtra, null, ref pcchOut); if (pcchOut == 0) { return(String.Empty); } var pszOut = new StringBuilder((int)pcchOut); // Allocate the output buffer AssocQueryString(assocF, assocStr, doctype, pszExtra, pszOut, ref pcchOut); // Get the full pathname to the program in pszOut string doc = pszOut.ToString(); return(doc); } catch (Exception exception) { Logger.Warn(exception); return(null); } }
/// <summary> /// Retrieves a file association for a particular extension /// From Ohad Schneider <see cref="!:https://stackoverflow.com/a/17773402"/> /// </summary> /// <param name="associationFlags">Any flags which need to be set in order to retrieve the association</param> /// <param name="associationType">The type of association which should be retrieved</param> /// <param name="fileExtension">The file association which we will use to determine the association</param> /// <returns></returns> private static string GetFileAssociation(AssocF associationFlags, AssocStr associationType, string fileExtension) { uint bufferSize = 0; uint returnedStatus = FileInvoker.AssocQueryString(associationFlags, associationType, fileExtension, null, null, ref bufferSize); const int successWithLength = 1; if (returnedStatus != successWithLength) { throw new NoAssociationFoundException( $"Could not determine the required buffer size required for the file association {associationType} and file extension {fileExtension}", fileExtension); } StringBuilder stringBuilder = new StringBuilder((int)bufferSize); returnedStatus = FileInvoker.AssocQueryString(associationFlags, associationType, fileExtension, null, stringBuilder, ref bufferSize); const int successWithAssociation = 0; if (returnedStatus != successWithAssociation) { throw new NoAssociationFoundException( $"Could not determine a string for the file association {associationType} and file extension {fileExtension}", fileExtension); } return(stringBuilder.ToString()); }
static extern uint AssocQueryString( AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, [In][Out] ref uint pcchOut);
public static extern uint AssocQueryString( AssocF flags, AssocStr str, [MarshalAs(UnmanagedType.LPWStr)] string pszAssoc, [MarshalAs(UnmanagedType.LPWStr)] string pszExtra, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszOut, ref uint pcchOut );
public static string AssocQueryString(AssocF assocF, AssocStr association, string assocString) { var length = 0u; var queryResult = AssocQueryString(assocF, association, assocString, null, null, ref length); if (queryResult != 1) { return(null); } var builder = new StringBuilder((int)length); queryResult = AssocQueryString(assocF, association, assocString, null, builder, ref length); return(queryResult == 0 ? builder.ToString() : null); }
internal static string AssocQueryString(AssocF assocF, AssocStr association, string assocString) { uint length = 0; uint ret = AssocQueryString(assocF, association, assocString, null, null, ref length); if (ret != 1) //expected S_FALSE { return null; //throw new InvalidOperationException("Could not determine associated string"); } var sb = new StringBuilder((int) length); //(length-1) will probably work too as null termination is added ret = AssocQueryString(assocF, association, assocString, null, sb, ref length); if (ret != 0) //expected S_OK { return null; //throw new InvalidOperationException("Could not determine associated string"); } return sb.ToString(); }
private static extern uint AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder sOut, [In][Out] ref uint nOut);
private static extern uint AssocQueryKey(AssocF flags, AssocKey key, string pszAssoc, string pszExtra, [Out] out UIntPtr phkeyOut);
public static string GetAssociation(AssocF assocF, AssocStr assocStr, string doctype) { try { uint pcchOut = 0; // size of output buffer // First call is to get the required size of output buffer AssocQueryString(assocF, assocStr, doctype, null, null, ref pcchOut); if (pcchOut == 0) { return String.Empty; } // Allocate the output buffer var pszOut = new StringBuilder((int)pcchOut); // Get the full pathname to the program in pszOut AssocQueryString(assocF, assocStr, doctype, null, pszOut, ref pcchOut); string doc = pszOut.ToString(); return doc; } catch (Exception exception) { Log.Error(exception); return null; } }
public static string GetAssoc(string pszAssoc , AssocF flags, AssocStr str) { uint pcchOut = 0; AssocQueryString(flags, str, pszAssoc, null, null, ref pcchOut); StringBuilder pszOut = new StringBuilder((int)pcchOut); AssocQueryString(flags, str, pszAssoc, null, pszOut, ref pcchOut); return pszOut.ToString(); }
private static extern uint AssocQueryString(AssocF flags, AssocStr str, string extension, string pszExtra, [Out] StringBuilder pszOut, ref uint pcchOut);