Solved

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

Posted on 2011-02-19
3
666 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
Comment Utility
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

Want to promote your upcoming event?

Are you going to an event? Are you going to be exhibiting at a tradeshow? Talking at a conference? Using a promotional banner in your email signature ensures that your organization’s most important contacts stay in the know and can potentially spread the word about the event.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now