简体   繁体   中英

path.join and electron.openExternal in win

const shell = require('electron').shell
shell.openExternal(path.join('base_path', 'child_path'))

If I join path like this in win,

the result of path.join('base_path', 'child_path') would be escaped and I can't open the link.

The separator(/) would be escaped.

I can open the link if I do like this.

shell.openExternal('base_path' + '/' + 'child_path')

But I think joining path like this could be dangerous.

Any way to solve this?

I want to use path.join().

If I understand your question, the way I've solved this is to use the toUnix method of the upath lib

upath.toUnix(upath.join(__dirname, "assets", "email.svg"));

Normal path doesn't convert paths to a unified format (ie /) before calculating paths (normalize, join), which can lead to numerous problems. Also path joining, normalization etc on the two formats is not consistent, depending on where it runs. Running path on Windows yields different results than when it runs on Linux / Mac.

In general, if you code your paths logic while developing on Unix/Mac and it runs on Windows, you may run into problems when using path.

Note that using Unix / on Windows works perfectly inside nodejs (and other languages), so there's no reason to stick to the Windows legacy at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM