Open a folder with explorer.exe

evilrat evilrat666 at gmail.com
Sun Dec 29 06:16:41 UTC 2024


On Saturday, 28 December 2024 at 19:25:48 UTC, bauss wrote:
> On Saturday, 28 December 2024 at 19:23:39 UTC, Jordan Wilson 
> wrote:
>> On Saturday, 28 December 2024 at 18:55:08 UTC, bauss wrote:
>>> I cannot figure out how to get spawnProcess working with 
>>> explorer.exe on Windows.
>>>
>>> Been trying to call it like:
>>>
>>> ```
>>> spawnProcess(["explorer.exe", path]);
>>> ```
>>>
>>> But it only opens explorer.exe in the documents path and not 
>>> the path given.
>>>
>>> I've tried to set the working directory too and it doesn't 
>>> work either.
>>>
>>> Can anyone help me? All I want to do is open a specific 
>>> folder in explorer.exe given a specific path.
>>
>> This may achieve what you want:
>> `spawnProcess(["powershell", "invoke-item", "-path", path]);`
>>
>> Thanks,
>>
>> Jordan
>
> Yeah I ended up doing something similar, but with cmd.

This have one downside, for example in real apps such as vscode 
going through right-click on folder and click "Show In Explorer" 
it will keep one instance of explorer, but with spawn process it 
doesn't tracks it and opens multiple explorer windows, so I use 
this snippet for windows.


```d
version(Windows) {
     import core.sys.windows.windows;
     import core.sys.windows.shlobj;
     alias PCIDLIST_ABSOLUTE = ITEMIDLIST*;
     alias PIDLIST_ABSOLUTE = ITEMIDLIST*;
     alias PCUITEMID_CHILD_ARRAY = LPITEMIDLIST*;
     extern(Windows) HRESULT 
SHOpenFolderAndSelectItems(PCIDLIST_ABSOLUTE pidlFolder, UINT 
cidl, PCUITEMID_CHILD_ARRAY apidl = null, DWORD dwFlags = 0);
     extern(Windows) PIDLIST_ABSOLUTE ILCreateFromPathW(PCTSTR 
pszPath);

     // implements "Open In Explorer" feature
     void browseToFile(string filename) {
         import std.utf;
         LPCTSTR _fpath = filename.toUTF16z;
         ITEMIDLIST *pidl = ILCreateFromPathW(_fpath);
         if (pidl) {
             SHOpenFolderAndSelectItems(pidl,0,null,0);
             ILFree(pidl);
         }
     }
}
```


More information about the Digitalmars-d-learn mailing list