Using environmental variables to get full path-names

Posted on 2000-02-14
Last Modified: 2010-05-02

I'm writing code which is designed to read a server name , which is a key-value in an INI file, and then use this to connect the PC running my application to this server.

To do this, I'm using the API function "GetPrivateProfileString" which works fine when I feed it the full path-name of my INI file. Unfortunately, when I release the software the pathname could be anywhere and is defined by the environmental variable "ServNme" - can I use "ServNme" as the "File" parameter in "GetPrivateProfileString" and if so what is the sytax please? Alternativey, is there any way I can find out the definition of "ServNme" on a PC and then plug the full path-name into "GetPrivateProfileString"?

Many thanks-

  ~Rob McClure
Question by:gizz
  • 2
LVL 70

Expert Comment

by:Éric Moreau
ID: 2519201
You can use the Environ function to read environmental variable.

See this example from the VB help:

Environ Function Example
This example uses the Environ function to supply the entry number and length of the PATH statement from the environment-string table.

Dim EnvString, Indx, Msg, PathLen   ' Declare variables.
Indx = 1   ' Initialize index to 1.
   EnvString = Environ(Indx)   ' Get environment
            ' variable.
   If Left(EnvString, 5) = "PATH=" Then   ' Check PATH entry.
      PathLen = Len(Environ("PATH"))   ' Get length.
      Msg = "PATH entry = " & Indx & " and length = " & PathLen
      Exit Do
      Indx = Indx + 1   ' Not PATH entry,
   End If   ' so increment.
Loop Until EnvString = ""
If PathLen > 0 Then
   MsgBox Msg   ' Display message.
   MsgBox "No PATH environment variable exists."
End If
LVL 32

Accepted Solution

Erick37 earned 50 total points
ID: 2519247
Environ("ServNme") will work, but if the ini file is in the same dir as the app, you can use App.Path
LVL 70

Expert Comment

by:Éric Moreau
ID: 2519269
Isn't it what I said!!!

Author Comment

ID: 2519295

I'm very grateful for your speedy answer but I couldn't get it to work - when I tried it I got back the message "No PATH environment variable exists"
Using the code 'Environ("ServNme")' was exactly what I needed - once again, thanks very much for the quick answer.

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

776 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