Jump to content

setting environment variables


Recommended Posts

Hello!

I want to set the environment variables for an user from cmd !

I found a command which set global system environment variable : "cmd /c reg add \"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\" /v \"{0}\" /d \"{1}\" /t REG_EXPAND_SZ"

Waiting your feedback!

Best regards,

Link to comment
Share on other sites

Hello!

I want to set the environment variables for an user from cmd !

I found a command which set global system environment variable : "cmd /c reg add \"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\" /v \"{0}\" /d \"{1}\" /t REG_EXPAND_SZ"

Waiting your feedback!

Best regards,

Ithe command which sets the user environment variable is:

"cmd /c reg add \"HKEY_CURRENT_USER\\Environment\" /v \"{0}\" /d \"{1}\" /t REG_EXPAND_SZ";

Link to comment
Share on other sites

Welcome to the Windows Forum.

Very interesting; thank you! Did you actually test it, and does it work?

Yes it is work I used both in a java program here is the code :

package engine;

import java.text.MessageFormat;

public class Environment

{

static final String REG_ADD_CMD = "cmd /c reg add \"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\" /v \"{0}\" /d \"{1}\" /t REG_EXPAND_SZ";

static final String REG_ADD_USER_CMD = "cmd /c reg add \"HKEY_CURRENT_USER\\Environment\" /v \"{0}\" /d \"{1}\" /t REG_EXPAND_SZ";

static String environmentVaiable = new String();

public Environment(){ }

void exec(String key, String value) throws Exception

{

// if (args.length != 2)

// throw new IllegalArgumentException("\n\nUsage: java SetEnv {key} {value}\n\n");

key = "testKeyU";

value = "testValueU";

String cmdLine = MessageFormat.format(REG_ADD_CMD, new Object[] { key, value });

Runtime.getRuntime().exec(cmdLine);

}

public void userExec(String key, String value) throws Exception

{

// if (args.length != 2)

// throw new IllegalArgumentException("\n\nUsage: java SetEnv {key} {value}\n\n");

key = "testKey";

value = "testValue";

String cmdLine = MessageFormat.format(REG_ADD_USER_CMD, new Object[] { key, value });

Runtime.getRuntime().exec(cmdLine);

}

public static String getEnv(String env){

environmentVaiable = System.getenv(env);

return environmentVaiable;

}

}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy