|
-
Feb 22nd, 2007, 09:01 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Environment Variables
I'm interfacing with a program that requires the use of the Path environment variable. My program has to modify the contents of this variable based on criteria selected by the user. So that I didn't have to parse/search through the path variable to find the part that I want, I created a new environment varibable named myPath. I add this to the Path variable by like this:
%myPath%.
This works fine but I want to eliminate the initialization setup where the user has to manually modify the path variable. I can create the new variable easily but how would I look to see if %myPath% is already in the path variable and if it isn't add it?
-
Feb 22nd, 2007, 09:43 AM
#2
Re: Environment Variables
While I don't see what would be complicated about parsing the path environment variable, since each entry is seperated by a semicolon, here is an article I had found on setting environment variables via VB.NET code.
http://jtbworld.blogspot.com/2005/11...ing-vbnet.html
-
Feb 22nd, 2007, 09:47 AM
#3
Re: Environment Variables
This code example shows how to check to see if an environment variable exists.
VB Code:
Dim strVariable As String = System.Environment.GetEnvironmentVariable("VARIABLE NAME")
If strVariable.Length = 0 Then
'Environment Variable does not exist
End If
-
Feb 22nd, 2007, 09:52 AM
#4
Thread Starter
Hyperactive Member
Re: Environment Variables
Thanks for the input.
The problem with parsing through the path variable is that I would need to know what the path was in order to search for it. I may not know this information. This is some of my real code:
VB Code:
strBinPath = GetEnvironmentVariable("binPath", User)
strBinPath = GetEnvironmentVariable("openPath", User)
SetEnvironmentVariable("openpath", strBinPath, Process)
SetEnvironmentVariable("binpath", strBinPath, Process)
the user is promted for the paths and the data is stored in an XML file.
VB Code:
dr("binpath") = frm.txtBinPath.Text
dr("openpath") = frm.txtOpenPath.Text
Then I manually set the path statement to include %openpath%;%binpath%.
I'm just trying to eliminate the manual step of adding the %mypath% to the the path statement. Can the % syntax but inserted with code?
-
Feb 22nd, 2007, 11:47 AM
#5
Thread Starter
Hyperactive Member
Re: Environment Variables
Here's the problem. Although I enter %opensimpath%;%binpath% in the Edit User Variable dialog box for the path variable that path variable contains the values of the variables not the link to them. If I use the code below, the message box is never seen because the % is not really part of the variable.
VB Code:
temp = GetEnvironmentVariable("path", User)
If InStr(temp, "%") > 0 Then
MessageBox.Show("found one")
End If
-
Feb 22nd, 2007, 12:43 PM
#6
Re: Environment Variables
1) Which version of VS are you using.
2) Why does it have to be a part of the Path environment?
-tg
-
Feb 22nd, 2007, 12:46 PM
#7
Thread Starter
Hyperactive Member
Re: Environment Variables
I'm using 2005.
The software that I'm interfacing with uses the Path Environment variable to find certain files. I need to to allow the user to change this path from my program but the other program will not know the path if it is not in the Path variable.
-
Feb 22nd, 2007, 12:51 PM
#8
Thread Starter
Hyperactive Member
Re: Environment Variables
This kind of explains what I'm looking to do. I need to view the unexpanded version of the path variable, look for my variable %myPath%, and add them if they do not exist.
VB Code:
Set objShell = WScript.CreateObject("WScript.Shell")
Set colEnvVars = objShell.Environment("User")
Wscript.Echo "Temporary folder (Unexpanded):"
Wscript.Echo colEnvVars("TEMP") & vbCrLf
Wscript.Echo "Temporary folder (Expanded)"
Wscript.Echo objShell.ExpandEnvironmentStrings("%TEMP%")
the output would be similar to this
Temporary folder (Unexpanded):
%USERPROFILE%\Local Settings\Temp
Temporary folder (Expanded)
C:\DOCUME~1\kmyer\LOCALS~1\Temp
-
Feb 22nd, 2007, 01:00 PM
#9
Thread Starter
Hyperactive Member
I'm a bone head
Sorry for wasting yor time here's the REALLY simple answer:
VB Code:
temp = GetEnvironmentVariable("path", User)
If InStr(temp, "%") = 0 Then
SetEnvironmentVariable("path", temp & ";%myPath%", User)
End If
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|