{"id":323,"date":"2019-09-25T11:01:59","date_gmt":"2019-09-25T17:01:59","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=323"},"modified":"2019-11-19T09:52:40","modified_gmt":"2019-11-19T16:52:40","slug":"showing-a-file-in-windows-explorer","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2019\/09\/25\/showing-a-file-in-windows-explorer\/","title":{"rendered":"Showing a file in Windows Explorer"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">No. Not IE. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ there are issues with this style\n\/\/  1. you can only select one file\n\/\/  2. If folder is already opened, it doesn't select the file\nDeclare Function ShellExecuteW lib \"shell32\" (hwnd as Integer, lpOperation as WString, lpFile as WString, lpParameters as WString, lpDirectory as Integer, nShowCmnd as Integer) as Integer\n\nDim err as Integer\nDim param As String\n\nparam = \"\/select, \"\"\" + f.AbsolutePath + \"\"\"\"\n\nerr = ShellExecuteW(Window(0).WinHWND, \"Open\", \"explorer\", param, 0, 1)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">EDIT &#8211; <a href=\"https:\/\/forum.xojo.com\/conversation\/post\/455369\">a better version<\/a> has been written by Julian Samphire<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">He&#8217;s given me permission to post it here. Note there are two methods. One takes an array of file paths. The second overloads things to accept  paramarry that then calls into the version that takes an array<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Public Function ShowSelectedInExplorer(path As String, files() As String) as Int32\n  Declare Function CoInitialize Lib \"Ole32.dll\" Alias \"CoInitialize\" (pvReserved As Integer) As Int32\n  Declare Function ILCreateFromPathW Lib \"Shell32.dll\" Alias \"ILCreateFromPathW\" (pszPath As WString) As Ptr\n  Declare Function SHOpenFolderAndSelectItems Lib \"Shell32.dll\" Alias \"SHOpenFolderAndSelectItems\" (pidlFolder As Ptr, cidl As UInt32, apidl As Ptr, dwFlags As UInt32) As Int32\n  Declare Sub ILFree Lib \"Shell32.dll\" Alias \"ILFree\" (pidl As Ptr)\n  \n  Call CoInitialize(0)\n  \n  Dim pidl As Ptr = ILCreateFromPathW(path)\n  If pidl = Nil Then \n    Return 0 'If path wasn't found then return\n  End If\n  \n  Dim mb As New MemoryBlock(COM.SIZEOF_PTR * (files.Ubound + 1))\n  \n  'Get a pidl for each file and store it\n  For i As Integer = 0 To files.Ubound\n    mb.Ptr(i * COM.SIZEOF_PTR) = ILCreateFromPathW(files(i))\n  Next\n  \n  Dim ok As Int32 = SHOpenFolderAndSelectItems(pidl, files.Ubound + 1, mb, 0)\n  \n  ILFree(pidl)\n  \n  Return ok\nEnd Function\n\n\nPublic Function ShowSelectedInExplorer(path As String, ParamArray files As String) as Int32\n  'Build an array from the param array and pass it back to ShowSelectedInExplorer so we can support both methods\n  Dim arrayOfFiles() As String\n  For Each f As String In files\n    arrayOfFiles.Append(f)\n  Next\n  Return ShowSelectedInExplorer(path, arrayOfFiles)\nEnd Function\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>No. Not IE. EDIT &#8211; a better version has been written by Julian Samphire He&#8217;s given me permission to post it here. Note there are two methods. One takes an array of file paths. The second overloads things to accept paramarry that then calls into the version that takes an array<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1],"tags":[6,31,3],"class_list":["post-323","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-tips","tag-windows","tag-xojo"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=323"}],"version-history":[{"count":3,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/323\/revisions"}],"predecessor-version":[{"id":332,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/323\/revisions\/332"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}