{"record":{"id":"66f2e477ebc89749","repo":"Flow-Launcher/Flow.Launcher","slug":"failed-to-open-folder-and-select-item","errorCode":null,"errorMessage":"Failed to open folder and select item","messagePattern":"Failed to open folder and select item","errorType":"exception","errorClass":"COMException","httpStatus":null,"severity":"warning","filePath":"Flow.Launcher.Infrastructure/Win32Helper.cs","lineNumber":877,"sourceCode":"        // https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shopenfolderandselectitems\n\n        public static unsafe void OpenFolderAndSelectFile(string filePath)\n        {\n            ITEMIDLIST* pidlFolder = null;\n            ITEMIDLIST* pidlFile = null;\n\n            var folderPath = Path.GetDirectoryName(filePath);\n\n            try\n            {\n                var hrFolder = PInvoke.SHParseDisplayName(folderPath, null, out pidlFolder, 0, out _);\n                if (hrFolder.Failed) throw new COMException(\"Failed to parse folder path\", hrFolder);\n\n                var hrFile = PInvoke.SHParseDisplayName(filePath, null, out pidlFile, 0, out _);\n                if (hrFile.Failed) throw new COMException(\"Failed to parse file path\", hrFile);\n\n                var hrSelect = PInvoke.SHOpenFolderAndSelectItems(pidlFolder, 1, &pidlFile, 0);\n                if (hrSelect.Failed) throw new COMException(\"Failed to open folder and select item\", hrSelect);\n            }\n            finally\n            {\n                if (pidlFile != null) PInvoke.CoTaskMemFree(pidlFile);\n                if (pidlFolder != null) PInvoke.CoTaskMemFree(pidlFolder);\n            }\n        }\n\n        #endregion\n\n        #region Win32 Dark Mode\n\n        /*\n         * Inspired by https://github.com/ysc3839/win32-darkmode\n         */\n\n        [DllImport(\"uxtheme.dll\", EntryPoint = \"#135\", SetLastError = true)]\n        private static extern int SetPreferredAppMode(int appMode);","sourceCodeStart":859,"sourceCodeEnd":895,"githubUrl":"https://github.com/Flow-Launcher/Flow.Launcher/blob/7fc63b07bbaa10a0f0b2601487913fcba9ed24b0/Flow.Launcher.Infrastructure/Win32Helper.cs#L859-L895","documentation":"Thrown as COMException with the selection HRESULT when SHOpenFolderAndSelectItems(pidlFolder, 1, &pidlFile, 0) returns a failed HRESULT, after both the folder and file PIDLs were successfully parsed. This is the actual 'open Explorer and highlight the item' call failing at the shell level despite valid inputs.","triggerScenarios":"Explorer is not running or is hung and cannot service the COM call; a Group Policy or shell restriction blocks programmatic folder selection; the item is in a virtual/zip namespace Explorer can't navigate to; the folder window creation failed due to a shell extension crash; repeated rapid calls exhaust Explorer's capacity.","commonSituations":"Explorer process replaced by a third-party file manager that doesn't implement SHOpenFolderAndSelectItems; a corporate policy disabling Explorer automation; Explorer hung from a bad shell extension; calling in a context without an STA / message pump; rapid-fire selects from a fast user action.","solutions":["Inspect the HRESULT (e.g. 0x80070005 access denied, 0x80004005 unspecified, 0x80040154 regdb_e_classnotreg) to classify.","Restart Explorer (taskkill /f /im explorer.exe then start it) if it is hung.","As a fallback, launch explorer.exe with the /select, argument: Process.Start(\"explorer.exe\", $\"/select,\\\"{filePath}\\\"\").","Ensure the call is made on a thread with the right apartment state and Explorer is running.","Check Group Policy for 'Remove Folder Options' / Explorer-restriction settings."],"exampleFix":"// before\nvar hrSelect = PInvoke.SHOpenFolderAndSelectItems(pidlFolder, 1, &pidlFile, 0);\nif (hrSelect.Failed) throw new COMException(\"Failed to open folder and select item\", hrSelect);\n\n// after — fall back to the /select command if the shell call fails\nvar hrSelect = PInvoke.SHOpenFolderAndSelectItems(pidlFolder, 1, &pidlFile, 0);\nif (hrSelect.Failed)\n{\n    Log.Warn(nameof(Win32Helper), $\"SHOpenFolderAndSelectItems failed: 0x{hrSelect:X}; using explorer /select\");\n    System.Diagnostics.Process.Start(\"explorer.exe\", $\"/select,\\\"{filePath}\\\"\");\n}","handlingStrategy":"fallback","validationCode":"null","typeGuard":"null","tryCatchPattern":"try { Win32Helper.OpenFolderAndSelectFile(filePath); }\ncatch (COMException ex) when (ex.Message.Contains(\"open folder and select\"))\n{ Log.Warn(...); Process.Start(\"explorer.exe\", $\"/select,\\\"{filePath}\\\"\"); }","preventionTips":["Always have a Process.Start(\"explorer.exe\", \"/select,...\") fallback when the shell COM call fails.","Restart Explorer if it is hung before retrying.","Ensure the call runs on a thread with appropriate apartment state.","Check Group Policy / third-party file-manager replacements that break SHOpenFolderAndSelectItems."],"tags":["win32","shell","com","explorer"],"backgroundTag":null,"analyzedSha":"7fc63b07bbaa10a0f0b2601487913fcba9ed24b0","analyzedAt":"2026-08-13T14:54:20.914Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}