-
Nov 20th, 2017, 04:30 PM
#1
Thread Starter
Fanatic Member
File Locations
I'm always coming in after programs are in place, so I have very little experience when it comes to distribution and installations, especially when it comes to File Locations and Environment Variables and the like.
The web offered up some different opinions / answers as to what and where things belong. As I typically don't get involved with installshield programs, I am looking for how to access and check the existence of files on a system.
Am curious about the proper Environment Variable to use for the following:
Code:
Components <system folder>
References <system folder>
Program Exe's %ProgramFiles%
Private Program Data %APPDATA%
Common Program Data %CommonProgramFiles%
Last edited by stuck-n-past; Nov 20th, 2017 at 04:36 PM.
-
Nov 20th, 2017, 10:51 PM
#2
Re: File Locations
Use the SHGetFolderLocation API.
Code:
' Add a command button to the form
Private Declare Function SHGetFolderLocation Lib "shell32" (ByVal hwndOwner As Long, ByVal nFolder As Long, ByVal hToken As Long, ByVal dwReserved As Long, pidl As Long) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Sub CoTaskMemFree Lib "ole32" (ByVal pvoid As Long)
Private Const MAX_PATH = 260
Private Const NOERROR = 0
Private Const SEP_DIR = "\" ' Directory separator character
Private Const SEP_URLDIR = "/" ' Separator for dividing directories in URL addresses.
Public Enum SpecialFolderIDs
sfidDESKTOP = &H0
sfidPROGRAMS = &H2
sfidPERSONAL = &H5
sfidFAVORITES = &H6
sfidSTARTUP = &H7
sfidRECENT = &H8
sfidSENDTO = &H9
sfidSTARTMENU = &HB
sfidDESKTOPDIRECTORY = &H10
sfidNETHOOD = &H13
sfidFONTS = &H14
sfidTEMPLATES = &H15
sfidCOMMON_STARTMENU = &H16
sfidCOMMON_PROGRAMS = &H17
sfidCOMMON_STARTUP = &H18
sfidCOMMON_DESKTOPDIRECTORY = &H19
sfidPROGRAM_FILES = &H26
sfidAPPDATA = &H1A
sfidPRINTHOOD = &H1B
sfidLOCAL_APPDATA = &H1C
sfidPROFILE = &H28
sfidProgramFiles = &H10000
sfidCommonFiles = &H10001
sfidFlagCreate = &H8000&
End Enum
Public Function GetSpecialFolder(nFolder As SpecialFolderIDs) As String
Dim sPath As String
Dim IDL As Long
If SHGetFolderLocation(0&, nFolder, 0&, 0&, IDL) = NOERROR Then
sPath = String$(MAX_PATH, 0)
SHGetPathFromIDList IDL, sPath
CoTaskMemFree IDL
GetSpecialFolder = StringFromBuffer(sPath)
AddDirSep GetSpecialFolder
End If
End Function
Public Function StringFromBuffer(buffer As String) As String
Dim nPos As Long
nPos = InStr(buffer, vbNullChar)
If nPos > 0 Then
StringFromBuffer = Left$(buffer, nPos - 1)
Else
StringFromBuffer = buffer
End If
End Function
Public Sub AddDirSep(strPathName As String)
strPathName = RTrim$(strPathName)
If InStr(strPathName, SEP_DIR & SEP_DIR) > 0 Then
strPathName = Left$(strPathName, 2) & Replace(Mid$(strPathName, 3), SEP_DIR & SEP_DIR, SEP_DIR)
End If
If Right$(strPathName, Len(SEP_URLDIR)) <> SEP_URLDIR Then
If Right$(strPathName, Len(SEP_DIR)) <> SEP_DIR Then
strPathName = strPathName & SEP_DIR
End If
End If
End Sub
Private Sub Command1_Click()
MsgBox "Program files: " & GetSpecialFolder(sfidPROGRAM_FILES) & vbCrLf & "Application data: " & GetSpecialFolder(sfidAPPDATA)
End Sub
-
Nov 21st, 2017, 08:18 AM
#3
Re: File Locations
Eduardo's suggestion of using an API is more reliable than using environment variables.
-
Nov 21st, 2017, 10:07 AM
#4
Re: File Locations
No need to write so much fiddly code, Shell32.dll gives us a fairly clean way to do this in VB6:
Code:
Option Explicit
'Reference to Microsoft Shell Control and Automation.
'
'Use late binding to avoid binary compatibility breaks after Windows XP
'but the typelib is still useful for the Shell Special Folder constants.
Private Sub Main()
'No ssf constant in the typelib for these, but the values are defined
'in SlhObj_core.h and are good here:
Const CSIDL_PROGRAM_FILES_COMMON = &H2B&
Const CSIDL_COMMON_DOCUMENTS = &H2E&
With CreateObject("Shell.Application")
MsgBox "ssfDESKTOP=" & .NameSpace(ssfDESKTOP).Self.Path _
& vbNewLine _
& "ssfCOMMONDESKTOPDIR =" & .NameSpace(ssfCOMMONDESKTOPDIR).Self.Path _
& vbNewLine _
& "ssfPERSONAL=" & .NameSpace(ssfPERSONAL).Self.Path _
& vbNewLine _
& "CSIDL_COMMON_DOCUMENTS=" _
& .NameSpace(CSIDL_COMMON_DOCUMENTS).Self.Path _
& vbNewLine _
& "ssfCOMMONAPPDATA=" & .NameSpace(ssfCOMMONAPPDATA).Self.Path _
& vbNewLine _
& "ssfAPPDATA=" & .NameSpace(ssfAPPDATA).Self.Path _
& vbNewLine _
& "ssfLOCALAPPDATA=" & .NameSpace(ssfLOCALAPPDATA).Self.Path _
& vbNewLine _
& "ssfPROGRAMFILES=" & .NameSpace(ssfPROGRAMFILES).Self.Path _
& vbNewLine _
& "CSIDL_PROGRAM_FILES_COMMON=" _
& .NameSpace(CSIDL_PROGRAM_FILES_COMMON).Self.Path
End With
End Sub
Result for user "George" here:
Note that in most cases you don't want to put data in the roaming profile. Use ssfLOCALAPPDATA for most purposes rather than ssfAPPDATA.
-
Nov 21st, 2017, 02:13 PM
#5
Thread Starter
Fanatic Member
Re: File Locations
Hey all thanks for the sample code, it's really helped in preventing me from 'hard' codding any of these directories.
While my initial question is pretty much solved, there's a lingering issue with regards to the actual format of the environment variable names.
Under My Computer, most entries are just plain Alphanumeric characters, but then others are sandwiched with percent signs. Lastly when running through documentation, special directories appear to make use of less-than and great-than symbols.
Code:
Plain Path Environ Environment Variable
% %SystemRoot% ExpandEnvironmentStrings
< > <system folder> SHGetPathFromIDList Special Directories
Are these all considered environment variables?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|