How do I open a file saved in the user directory using Electron's Shell API

I’d like an Electron app to natively open a file stored in the User directory, with the path as %userprofile%/Documents/Dir1/ThisFile.txt.

From Electron’s Shell Documentation:

const { shell } = require('electron')

shell.openPath('%userprofile%/Documents/Dir1/ThisFile.txt')

yields an error:

Windows cannot find ‘%userprofile%/Documents/Dir1/ThisFile.txt’. Make sure you typed the name correctly and try again.

I have also tried using:
shell.openPath(path.relative('./','%userprofile%/Documents/Dir1/ThisFile.txt')); which results in the same error.

What am I doing wrong? Is there a way to open a file stored in %userprofile%.

76 thoughts on “How do I open a file saved in the user directory using Electron's Shell API”

Leave a Comment