NodeJs - 如何在 NodeJs 中获取 Windows ProgramData 目录

NodeJs - How to get Windows ProgramData directory in NodeJs

我需要将一些文件写入文件夹,任何本地用户都可以访问这些文件,并且任何用户都可以修改这些文件。 所以我决定使用 Windows ProgramData 文件夹。我从 article.

中找到了以下详细信息

There’s also the ProgramData folder. This folder has most in common with the Application Data folders, but—instead of having an individual folder for each user—the ProgramData folder is shared among all the user accounts on your PC.

On Windows XP, there was no C:\ProgramData folder. Instead, there was a “C:\Documents and Settings\All Users\Application Data” folder. Starting with Windows Vista, the All Users application data folder was moved to C:\ProgramData.

You can still see this today. If you plug C:\Users\All Users\ into File Explorer or Windows Explorer on Windows 10, Windows will automatically redirect you to the C:\Program Data folder. It’ll redirect any program that tries to write to C:\Users\All Users\ to the C:\ProgramData folder, too.

有没有什么方法可以在 NodeJs 中获取 Windows ProgramData 目录,而无需像 "C:\ProgramData""C:\Users\All Users\" 这样的硬编码?

您可以使用环境变量ProgramData。参见

console.log(process.env.ProgramData);

也许this文章能帮到你。

它通过使用 process.env 属性:

解决了问题

The process.env property is an inbuilt application programming interface of the process module which is used to get the user environment.

您应该从“ALLUSERPROFILE”中获取“C:\ProgramData”,如下所示:

// Include process module 
const process = require('process'); 
// Printing process.env property value 
var env = process.env; 
console.log("alluserprofile: " + env.ALLUSERSPROFILE);