Results 1 to 9 of 9

Thread: [RESOLVED] Environment Variables

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Resolved [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?

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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

  3. #3
    Hyperactive Member ProphetBeal's Avatar
    Join Date
    Aug 2006
    Location
    New York
    Posts
    424

    Re: Environment Variables

    This code example shows how to check to see if an environment variable exists.
    VB Code:
    1. Dim strVariable As String = System.Environment.GetEnvironmentVariable("VARIABLE NAME")
    2. If strVariable.Length = 0 Then
    3.     'Environment Variable does not exist
    4. End If

    Helpful Links:
    VB 6 - How to get the "Key" Value in a collection
    VB.NET - File Search Utility || VB.NET - How to compare 2 directories || VB.NET - How to trust a network share
    VB.NET - Create Excel Spreadsheet From Array || VB.NET - Example Code & Hints you may not know
    VB.NET - Save JPEG with a certain quality (image compression) || VB.NET - DragDrop Files, Emails, and Email Attachments

    Please post some of the code you need help with (it makes it easier to help you)
    If your problem has been solved then please mark the thread [RESOLVED].
    Don't forget to Rate this post

    "Pinky, you give a whole new meaning to the phrase, 'counter-intelligence'."-The Brain-

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    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:
    1. strBinPath = GetEnvironmentVariable("binPath", User)
    2.         strBinPath = GetEnvironmentVariable("openPath", User)
    3.  
    4.         SetEnvironmentVariable("openpath", strBinPath, Process)
    5.         SetEnvironmentVariable("binpath", strBinPath, Process)

    the user is promted for the paths and the data is stored in an XML file.

    VB Code:
    1. dr("binpath") = frm.txtBinPath.Text
    2. 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?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    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:
    1. temp = GetEnvironmentVariable("path", User)
    2.         If InStr(temp, "%") > 0 Then
    3.             MessageBox.Show("found one")
    4.         End If

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    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.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    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:
    1. Set objShell = WScript.CreateObject("WScript.Shell")
    2. Set colEnvVars = objShell.Environment("User")
    3. Wscript.Echo "Temporary folder (Unexpanded):"
    4. Wscript.Echo colEnvVars("TEMP") & vbCrLf
    5. Wscript.Echo "Temporary folder (Expanded)"
    6. 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

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    I'm a bone head

    Sorry for wasting yor time here's the REALLY simple answer:

    VB Code:
    1. temp = GetEnvironmentVariable("path", User)
    2.         If InStr(temp, "%") = 0 Then
    3.             SetEnvironmentVariable("path", temp & ";%myPath%", User)
    4.         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
  •  



Click Here to Expand Forum to Full Width