问题
I in electron am doing:
path.dirname('C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron\\main')
That path is the actual value of my __dirname
. How come it is not giving me C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron
? I want that main
part chopped off.
回答1:
Assuming main
is a directory
inside electron
. Also assuming that you have some file called index.js
inside main
folder where you want to have the path of electron directory
.
So, you can do path.join this way:
var mainFolderParentPath = path.join(__dirname, '../');
Your original file location:
C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron\\main\\index.js
__dirname will return
C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron\\main
and then inside path.join '../', will chop off the main folder from path. So, you will be left off with:
C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron
回答2:
Well you obviously didn't read the docs for dirname. It states that it works like the Unix command dirname
which "strips non-directory suffix from file name", thus you get the C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron
.
What you are looking for is basename.
path.basename('C:\\Users\\Blagoh\\Documents\\GitHub\\Screeenshoter\\dist\\electron\\main')
will give you main
.
来源:https://stackoverflow.com/questions/45261574/path-dirname-on-windows-path-is-giving