Solved

How to Create an Environment Variable using Visual Basic 6 VB6 in Windows XP?

Posted on 2011-02-19
3
674 Views
Last Modified: 2012-08-13
I need to create an Environment Variable under system variables that has this information:
Variable
LOCALDIR
Value:
Z:\Test\VB

My skills in VB are limited so please post the easiest code, Thanks.
I am using Visual Basic 6pro VB6 and Windows XP Pro.
0
Comment
Question by:daneburr
3 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 20 total points
ID: 34934446
A bit of a hack, but this is how I would do it:

There is a DOS command called "REG" that you can use to directly add the new environment variable definition to the Windows registry.   This is the DOS command that does it (substitute your own symbol name and value, of course).

REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "mynewsystemsymbol" /t REG_SZ /d "mynewvalue" /f

Open in new window


[Once the new environment variable has been defined, having it take effect without logging out and back in again is another question.]

Construct the DOS "REG" command in a string variable and use the "shell" function to execute it.

Dim mySetEnvCommand As String
mySetEnvCommand = "REG ADD ""HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"" /v """ & mynewsystemsymbol & """ /t REG_SZ /d """ & mynewvalue & """ /f"

shell(mySetEnvCommand)

Open in new window

0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question