Go Premium for a chance to win a PS4. Enter to Win


Using environmental variables to get full path-names

Posted on 2000-02-14
Medium Priority
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 200 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…
Suggested Courses

916 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