ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

node打开本地应用程序

2021-01-29 17:36:02  阅读:248  来源: 互联网

标签:node const err process app 应用程序 caption 本地 open


1.打开浏览器

最简单的方法:

const cp = require('child_process')
cp.exec('start http://127.0.0.1:8889/');  // 自动打开默认浏览器

另一种方法是安装open 依赖包:

 
const open = require('open');

(async () => {
    // Opens the image in the default image viewer and waits for the opened app to quit.
    await open('unicorn.png', {wait: true});
    console.log('The image viewer app quit');

    // Opens the URL in the default browser.
    await open('https://sindresorhus.com');

    // Opens the URL in a specified browser.
    await open('https://sindresorhus.com', {app: 'firefox'});

    // Specify app arguments.
    await open('https://sindresorhus.com', {app: ['google chrome', '--incognito']});
})();
 

可以看到支持的功能更全面,对各平台的支持也有保证。

2.打开指定的应用程序

这里打开VS CODE

const exec = require('child_process').execFile;

const path = "D:\\Program Files\\Microsoft VS Code\\Code.exe" 
exec(path, function(err, data) { if (err) { throw err; } console.log(data.toString()); });

将会打开VS CODE

在windows下怎么获取程序的运行参数呢?

可以先右键用vs code打开一个项目

然后win + r 打开 运行程序

输入wmic 回车

查看所有运行中进程的命令行参数:

wmic process get caption,commandline /value

查询指定进程的命令行参数:

wmic process where caption="notepad.exe" get caption,commandline /value【精确查找】

wmic process where="caption like 'notepad%'" get caption,commandline /value【模糊查找】

 所以,打开指定的目录就可以通过如下代码实现了:

const exec = require('child_process').execFile;

const path = "D:\\Program Files\\Microsoft VS Code\\Code.exe"
exec(path, ['目录路径'], function(err, data) {  
        if (err) {
           throw err;
        }
        console.log(data.toString());                       

});   
关于node的一些应用:https://www.cnblogs.com/cangqinglang/category/1207611.html?page=1
文章转自:https://www.cnblogs.com/cangqinglang/p/12492478.html

标签:node,const,err,process,app,应用程序,caption,本地,open
来源: https://www.cnblogs.com/sweeeper/p/14346136.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有