typescript 如何获取应用程序使用打字稿运行的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37213696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the path that the application is running with typescript?
提问by geo
I am trying to create a desktop application with electron, angular2, typescript and neDB.In order to be able create a 'file' database with neDB I want the path to my project.How can I get this with typescript ?
我正在尝试使用电子、angular2、打字稿和 neDB 创建一个桌面应用程序。为了能够使用 neDB 创建一个“文件”数据库,我想要我的项目的路径。我怎样才能用打字稿获得这个?
回答by Dan
Use app.getAppPath()
Typescript is a superset of javascript so you could do it in the same way you would do it with javascript, though you may want to declare typings, or use other typescript features when you do so.
Typescript 是 javascript 的超集,因此您可以使用与使用 javascript 相同的方式来执行它,尽管您可能想要声明类型,或者在这样做时使用其他 typescript 功能。
Example:
例子:
const remote = require('remote'),
app = remote.require('app');
var basepath = app.getAppPath();
Update - these days you should use:
更新 - 这些天你应该使用:
const app = require('electron').remote.app
To get the app handle for app.getAppPath()
.
获取app.getAppPath()
.
回答by Vadim Macagon
Writing data to the application installation directory is generally a bad idea since the user running the app may not have permission to write files to that directory. What you should probably do instead is create the database file at the location returned by app.getPath('userData')
.
将数据写入应用程序安装目录通常是一个坏主意,因为运行应用程序的用户可能没有将文件写入该目录的权限。您可能应该做的是在返回的位置创建数据库文件app.getPath('userData')
。
回答by Jimmy Pickboy
Here is what worked for me:
这是对我有用的:
require('electron').remote.app.getAppPath()