[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-08-13
6
Medium Priority
?
520 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…
Suggested Courses

834 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