如何获取nw.js可执行文件的当前目录 [英] How to get the current directory for an nw.js executable

查看:723
本文介绍了如何获取nw.js可执行文件的当前目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建nw.js可执行文件时,努力查找当前目录.

Struggling to find the current directory when I create an nw.js executable.

我创建了一个包含html和Javascript以及大量node.js模块用法的Mac服务器应用程序.我有两个可供服务器访问的xml文件,它们与应用程序位于同一文件夹中.

I have created a Mac server application with a mix of html and Javascript and a lot of node.js module usage. I have two xml files that the server accesses and that reside in the same folder as the application.

当我按如下方式运行应用程序时: ./nwjs.app/Contents/MacOS/nwjs. 并找到当前目录,如下所示: var workingFolder = process.cwd(); 然后我的工作文件夹就是应用程序和xml文件所在的文件夹.

When I run the application as follows: ./nwjs.app/Contents/MacOS/nwjs . and find the current directory as follows: var workingFolder = process.cwd(); then my working folder is the one where the application and xml files reside.

但是,如果我遵循打包和分发"指南(创建一个.nw文件,将其命名为app.nw,然后放入内容/资源中),然后运行该.app可执行文件,则得到以下内容:工作文件夹: /private/var/folders/3x/2hq40kzs59q8gj6yfn4ktj940000gn/T/.io.nwjs.nw.HcwnF2

However, if I follow the 'package and distribute' guidelines (create a .nw file, name it app.nw, and put in contents/resources), and then run the .app executable, I get the following as the working folder: /private/var/folders/3x/2hq40kzs59q8gj6yfn4ktj940000gn/T/.io.nwjs.nw.HcwnF2

我认为上下文已更改.因此,我一直在寻找获取当前目录的替代方法.我试过了: var workingFolder = global .__ dirname; 并得到相同的结果.然后: var workingFolder = process.env.PWD; 并没有定义(尽管这适用于未打包"版本)

I assume that the context changed. So I have been looking at alternatives to get the current directory. I tried: var workingFolder = global.__dirname; and got the same result. Then: var workingFolder = process.env.PWD; and got undefined (although this worked for the 'unpackaged' version)

如何获取当前目录?

任何想法将不胜感激!

推荐答案

对于Windows使用:var path = require('path'); path.dirname(process.execPath);

For Windows use: var path = require('path'); path.dirname(process.execPath);

对于Mac和Linux,请使用:global.__dirname;

For Mac and Linux use: global.__dirname;

这篇关于如何获取nw.js可执行文件的当前目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆