public static string GetDefaultProgramPathRelated(string Path) { for (uint BufferSize = 512; ; BufferSize += 512) { StringBuilder Builder = new StringBuilder(Convert.ToInt32(BufferSize)); HRESULT Result = ShlwApi.AssocQueryString(ShlwApi.ASSOCF.ASSOCF_NOFIXUPS | ShlwApi.ASSOCF.ASSOCF_VERIFY | ShlwApi.ASSOCF.ASSOCF_NOTRUNCATE | ShlwApi.ASSOCF.ASSOCF_INIT_DEFAULTTOSTAR | ShlwApi.ASSOCF.ASSOCF_REMAPRUNDLL, ShlwApi.ASSOCSTR.ASSOCSTR_EXECUTABLE | ShlwApi.ASSOCSTR.ASSOCSTR_APPID, System.IO.Path.GetExtension(Path).ToLower(), null, Builder, ref BufferSize); if (Result == HRESULT.S_OK) { string ExePath = Builder.ToString(); if (System.IO.Path.IsPathRooted(ExePath)) { return(ExePath); } else { return(ExePath.Replace("@", string.Empty).Replace("{", string.Empty).Replace("}", string.Empty).Split('?').FirstOrDefault()); } } else if (Result == HRESULT.E_POINTER) { continue; } else { return(string.Empty); } } }
private static IShellItem2 GetShellItemFromCommandLine() { var ppszCmd = Environment.GetCommandLineArgs(); var szSpec = new StringBuilder(ppszCmd.FirstOrDefault(s => s.Length > 1 && s[0] != '-' && s[0] != '/')); ShlwApi.PathUnquoteSpaces(szSpec); if (string.IsNullOrEmpty(szSpec.ToString())) { return(null); } var hr = SHCreateItemFromParsingName(szSpec.ToString(), null, typeof(IShellItem2).GUID, out var ppv); return(hr.Succeeded ? (IShellItem2)ppv : SHCreateItemFromParsingName <IShellItem2>(Environment.CurrentDirectory)); }
public static string GetAssociate(string Path) { Task <string> RunTask = Task.Run(() => { try { uint Length = 0; if (ShlwApi.AssocQueryString(ShlwApi.ASSOCF.ASSOCF_NOFIXUPS | ShlwApi.ASSOCF.ASSOCF_VERIFY, ShlwApi.ASSOCSTR.ASSOCSTR_EXECUTABLE, System.IO.Path.GetExtension(Path), null, null, ref Length) == HRESULT.S_FALSE) { StringBuilder Builder = new StringBuilder((int)Length); if (ShlwApi.AssocQueryString(ShlwApi.ASSOCF.ASSOCF_NOFIXUPS | ShlwApi.ASSOCF.ASSOCF_VERIFY, ShlwApi.ASSOCSTR.ASSOCSTR_EXECUTABLE, System.IO.Path.GetExtension(Path), null, Builder, ref Length) == HRESULT.S_OK) { return(Builder.ToString()); } else { return(string.Empty); } } else { return(string.Empty); } } catch { return(string.Empty); } }); if (SpinWait.SpinUntil(() => RunTask.IsCompleted, 3000)) { return(RunTask.Result); } else { return(string.Empty); } }