Solved

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

Posted on 2004-08-13
6
508 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now