electron: [Bug]: shell.openPath open windows explorer in the background (windows 11 update 22H2)

Preflight Checklist

Electron Version

22.0.0

What operating system are you using?

Windows

Operating System Version

22621.963

What arch are you using?

x64

Last Known Working Electron version

No response

Expected Behavior

shell.openPath should open windows explorer on top of the electron app.

Actual Behavior

shell.openPath open windows explorer behind the electron app.

Testcase Gist URL

No response

Additional Information

When this issue was discovered, the electron app was using shell.openItem. I updated electron to 22.0.0 and updated shell.openPath from (*.openItem) but it is the same behavior. I tested on different Windows 11 22H2 machines and I have encountered the same issue. I tried shell.showItemInFolder() and shell.openExternal() but the same issue occured. Before Windows 11 22H2 there was a registry key connected with foreground (HKCU\Control Panel\Desktop\ForegroundLockTimeout set to 0; ForegroundFlashCount set to 7) that would help to make sure windows explorer appear in front of the electron app but this is not the case anymore; after some research I did not find any information on some new registry key that could help. After reading this issue (https://github.com/electron/electron/issues/4349) I realized it is probably a similar problem with electron. Please advise.

I found a way where shell.openPath() open in front of the electron app, in Windows 11 22H2. However, I would like to make sure this is a safe way to do it. Please let me know if it is not. I am not really yet so comfortable with the IPC shenanigans…

Here is the code that for me resolve the issue : // Renderer

        try {
          ipcRenderer.send('openExplorer', docName);
        }
        catch(errorMsg) {
            /*log it*/  
        }

// Main.js

ipcMain.on('openExplorer', (event, pathToOpenExplorerIn) => {
    try {
      if (testSender(event.senderFrame.url)){
        shell.openExternal(pathToOpenBrowserIn);
      }
    }
    catch(error) {
      /*log it*/ 
    }
})

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 16 (1 by maintainers)

Most upvoted comments

This issue has been closed due to inactivity, and will not be monitored. If this is a bug and you can reproduce this issue on a supported version of Electron please open a new issue and include instructions for reproducing the issue.

I had the same problem version: electron20.0.0

Hello,

I noticed you were looking for a solution to your issue. I believe the following code snippet could be beneficial for addressing the problem you’ve described. This code provides a way to handle file operations such as showing an item in a folder or opening a path directly from the Electron main process, making these operations accessible to the renderer process through a secure, context-isolated bridge.

// main process
const { app, ipcMain, shell } = require('electron');

app.on('ready', async () => {
  ipcMain.handle('showItemInFolder', (event, fullPath) => {
    shell.showItemInFolder(fullPath);
  });
  ipcMain.handle('openPath', (event, fullPath) => {
    shell.openPath(fullPath);
  });
});

// preload.js
const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('electronAPI', {
  showItemInFolder(fullPath) {
    return ipcRenderer.invoke('showItemInFolder', fullPath);
  },
  openPath(path) {
    return ipcRenderer.invoke('openPath', path);
  }
});

To use this functionality in your renderer process, you can call the methods provided by window.electronAPI like so:

window.electronAPI.showItemInFolder('path/to/your/file');
window.electronAPI.openPath('path/to/your/folder');

This setup ensures that your application can perform these operations safely and efficiently, leveraging Electron’s IPC (Inter-Process Communication) for secure communication between the renderer and the main process. Let me know if this helps or if you need further assistance!

Best regards,

zhangsan

//in your main process
ipcMain.handle("process-message", (_, path) => {
    shell
      .openPath(path)
      .then(() => {
        return true;
      })
      .catch(() => {
        return false;
      });
});
//in your renderer process
ipcRenderer.invoke("process-message", path).then(res=>{
      if(res){
                //success
      }else{
               //error
      }
})

Any fix for this? Same issue with 25.3.1. const normalizedPath = path.normalize(inputPath); did not work for me.

I know only a workaround. Something like that:

if(process.platform === 'win32'){
        // Workround
        spawn('explorer.exe', [url]);
      } else {
        shell.openPath(url);
      }