Node.Js-获取Windows用户名 [英] Node.Js - Get windows username

查看:796
本文介绍了Node.Js-获取Windows用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取运行node.jS应用程序的计算机的Windows用户名(该应用程序始终在Windows计算机上运行).

I am trying to get the windows username of the machine that is running my node.jS app (the app is always running on a Windows machine).

如何使用Node.Js获取当前的Windows用户名?

How can I get the current windows username using Node.Js?

我试图在C#中为node.js找到与 WindowsIdentity.GetCurrent().Name 类似的东西,或者在CMD中找到与 whoamI 命令相似的东西.

I am trying to find something similar to WindowsIdentity.GetCurrent().Name in C# for node.js, or whoamI command in CMD.

推荐答案

自Node v6(2016-04)起,您可以使用

Since Node v6 (2016-04), you can just use os.userInfo :

var os = require('os');
os.userInfo().username


要获取当前登录的用户名:


To get the current logged-in username:

var path = require('path');
var userName = process.env['USERPROFILE'].split(path.sep)[2];
var loginId = path.join("domainName",userName);
console.log(loginId);

这篇关于Node.Js-获取Windows用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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