Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I get the directory of program files folder fairly easily

Posted on 2004-08-13
6
Medium Priority
?
517 Views
Last Modified: 2008-02-01
Hello all,
I am very new to VB and programming in general. I am attempting to define the path to a file in the program files directory for usage in other parts of the program. So in the Genral Declarations section I put this statement
Private Const INI_FILE As String = "C:\Program Files\Data\data.ini"
 So then I can just refer INI_FILE whenever I want to write or read from this file, esp. when using the read/write ini api's.
The obvious problem is that if a user has a non-standard setup, like their program files folder exists on the D drive or something then this will fail because its set as a constant. What can I do to declare the path to this file with a variable included. Like
INI_FILE = progfiles & "\Data\data.ini"  
If it try this I get an "Invalid Ouside procedure" error.
I know there is an API call that will get the Program Files directory for me, but how do I add it into this? Thanks in advance!
0
Comment
Question by:JSunn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 19

Expert Comment

by:Shauli
ID: 11794692
Locate the file in the progrma's folder under Program files, and do:

App.Path & "\Data\data.ini"

Where app.path is the folder from which the program is running.

S


0
 
LVL 2

Author Comment

by:JSunn
ID: 11794745
I am getting Invalid outside procedure, remeber this statement is in the genral declarations
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11794822
You can read it from registry
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Shauli
ID: 11794915
Hardcoding a path and file name is not recommended. What if tomorrow you change either the path or the file name. There are several ways to store the ini file name. One is in a database. Another would be in a txt file, and another would be in the registry. Speaking of which, ini files are being used only for a VERY VERY large amount of parameters. Otherwise, we dont use ini files at all, instead we store all the parameters in the registry. A simple way to do it, is to use the GetSetting and SaveSetting built in functions of VB. Using these functions you store the keys in the CURRENT_USER/Software/VB and VBA Applications/YourappName.
Just keep in mind that using this methid is PER USER loged on the machine.

Let us know if you need code example of this method.

S
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11794929
Also, you can try this (API):


Private Declare Function SHGetSpecialFolderPath _
   Lib "shell32.dll" _
   Alias "SHGetSpecialFolderPathA" _
   (ByVal hWnd As Long, _
   ByVal lpszPath As String, _
   ByVal nFolder As Integer, _
   ByVal fCreate As Boolean) As Boolean

Private Const CSIDL_PROGRAM_FILES = &H26


Private Sub Form_Load()
 Dim blnReturn As Long
   Dim strBuffer As String
   strBuffer = Space(255)
   blnReturn = SHGetSpecialFolderPath(0, _
      strBuffer, _
      CSIDL_PROGRAM_FILES, _
      False)
     
   strBuffer = Left(strBuffer, InStr(strBuffer, Chr(0)) - 1)
   MsgBox strBuffer
End Sub
0
 
LVL 19

Accepted Solution

by:
Shauli earned 500 total points
ID: 11794966
ps. If you still want to hardcode your ini path and file name, then simply do this as your first line in the form load event:

myIniFile = App.Path & "\Data\data.ini"

Where myIniFile would be declared as public varibal in the declaration area, and peace will be on earth :)

S
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

610 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