node.jswindowsapplication-datawindows-usersprogramdata

NodeJs - How to get Windows ProgramData directory in NodeJs


I need to write some files into folder, that can be access any local user and can be modified by any user. So I decided to use Windows ProgramData folder. I found following details from a 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.

Is there any method to get Windows ProgramData directory in NodeJs, without hard coding like "C:\ProgramData" or "C:\Users\All Users\" ?


Solution

  • Maybe this article can help you.

    It solves the problem by using the process.env property:

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

    You should get "C:\ProgramData" from "ALLUSERPROFILE" as follows:

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