Solved

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

Posted on 2004-08-13
6
512 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
  • 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 125 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

680 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