Solved

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

Posted on 2004-08-13
6
509 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

912 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

17 Experts available now in Live!

Get 1:1 Help Now