gpt4 book ai didi

How to grab the computer name with NodeJs / Electron?(如何用NodeJS/Electron抓取计算机名?)

转载 作者:bug小助手 更新时间:2023-10-25 09:22:39 31 4
gpt4 key购买 nike



I am trying to retrieve the computer name from my computer so I can use it in comparison in my mysql query.

我正在尝试从我的计算机中检索计算机名称,以便在MySQL查询中进行比较。



I would like to define computerName as a variable that grabs the user-defined computer name using NodeJs. This application is being run via electron for windows.

我想将Computer Name定义为一个变量,该变量使用NodeJS获取用户定义的计算机名称。这个应用程序是通过电子视窗运行的。



Any help would be greatly appreciated.

任何帮助都将不胜感激。


更多回答
优秀答案推荐

You can use the standard nodeJS os API to retrieve the (host)name of a computer, see this API documentation for more details.

您可以使用标准的NodeJS os API来检索计算机的(主机)名称,有关更多详细信息,请参阅此API文档。



const computerName = os.hostname()


On a Mac the only way to reliably get the "ComputerName" as OP requested is to use a child process, which isn't ideal...

在Mac上,可靠地获取OP请求的“ComputerName”的唯一方法是使用子进程,这并不理想……



cp.execSync('scutil --get ComputerName')

cp.execSync('scutil--get ComputerName')



Most of the time hostname will === ComputerName, but I found out the hard way today that this is not always the case.

大多数情况下,主机名将=ComputerName,但我今天很痛苦地发现,情况并不总是如此。



const cp = require('child_process')
const os = require('os')

function getComputerName() {
switch (process.platform) {
case "win32":
return process.env.COMPUTERNAME;
case "darwin":
return cp.execSync("scutil --get ComputerName").toString().trim();
case "linux":
const prettyname = cp.execSync("hostnamectl --pretty").toString().trim();
return prettyname === "" ? os.hostname() : prettyname;
default:
return os.hostname();
}
}

console.log("getComputerName", getComputerName());

Inspiration Ref: https://github.com/egoist/computer-name

参考文献:https://github.com/egoist/computer-name


更多回答

For anyone coming here, I just discovered that hostname does not always equal computerName. It's causing me some headaches right now as we have a fleet of Macs which do not match.

对于任何来到这里的人,我刚刚发现主机名并不总是等于计算机名。这让我现在有些头疼,因为我们有一队不匹配的Mac电脑。

Excellent answer as sometimes networks will return a different name than is expected.

回答得很好,因为有时网络会返回一个与预期不同的名称。

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com