[RESOLVED] Open a folder in %AppData%
Hi,
From Start, Run if I type:
explorer.exe %AppData%\FolderName
FolderName opens in Windows Explorer, but...
Code:
Shell "explorer.exe %AppData%\FolderName", 1
Returns an error message from Windows Explorer:
The path '%AppData%\FolderName' does not exist or is not a directory.
but...
Code:
Shell "explorer.exe c:\", 1
opens the root directory in explorer.
Can you tell why it won't recognize %AppData%?
Re: Open a folder in %AppData%
Because %AppData% means nothing in VB. You should use App.Path instead.
Shell "explorer.exe " & App.Path & "\FolderName", 1
Re: Open a folder in %AppData%
That is probably because under some OS' the AppData translates to another subdirectory (Roming, LocalLow, Local for ex.) below AppData.
So it may not be giving you the desired path you are expecting and can not open the folder since the path is invalid.
What OS?
Re: Open a folder in %AppData%
Thanks for the reply.
app.path points to the folder where my program is being run from, not the systems application data path under Documents and Settings, which is where %AppData% points too.
Re: Open a folder in %AppData%
RobDog888,
I've tested %AppData% in both XP and Vista Home Premium and it points where I want it too, I just need to know the VB equivalent.
Re: Open a folder in %AppData%
%AppData% is an environmental variable from the OS. Its valid in VB.
What OS?
Re: Open a folder in %AppData%
In XP it points to
C:\Documents and Settings\UserName\Application Data
In Vista it points to
C:\Users\UserName\AppData\Roaming
Different paths.
Re: Open a folder in %AppData%
Call me genius (I figured this out simply by trying typing a lot of things):
Code:
Option Explicit
Private Const SW_SHOWMAXIMIZED = 3
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Sub Form_Load()
ShellExecute 0, "explore", """shell:AppData""", vbNullString, vbNullString, SW_SHOWMAXIMIZED
End Sub
If you take the extra quotes out, it'll show you Move and Copy browse dialogs, so you must have the extra quotes around it. """shell:AppData\Program Name""" works as well.
Edit!
A (complete?) list of what you can use in Vista - remember to also check if it works in XP.
Re: Open a folder in %AppData%
Merri,
From now on I will call you a genius, works just the way I wanted.
Thank you.
Re: [RESOLVED] Open a folder in %AppData%
SHGetFolderPath API can return any XP-supported special folder (and its Vista counterpart), including both the user's Application Data as well as the shared Application Data folders. Here's a generic function you can use to identify them, along with an enumeration of all the supported special folders for ease of use:
Code:
Public Enum FolderEnum
feApp = 0 ' \Program Files\Project (more reliable than App.Path)
feCDBurnArea = 59 ' \Docs & Settings\User\Local Settings\Application Data\Microsoft\CD Burning
feCommonAppData = 35 ' \Docs & Settings\All Users\Application Data
feCommonDesktop = 25 ' \Docs & Settings\All Users\Desktop
feCommonDocs = 46 ' \Docs & Settings\All Users\Documents
feCommonPics = 54 ' \Docs & Settings\All Users\Documents\Pictures
feCommonMusic = 53 ' \Docs & Settings\All Users\Documents\Music
feCommonStartMenu = 22 ' \Docs & Settings\All Users\Start Menu
feCommonStartMenuPrograms = 23 ' \Docs & Settings\All Users\Start Menu\Programs
feProgramFiles = 38 ' \Program Files
feProgramFilesCommon = 43 ' \Program Files\Common Files
'feRecycleBin = 10 ' ???
feUser = 40 ' \Docs & Settings\User
feUserAppData = 26 ' \Docs & Settings\User\Application Data
feUserCache = 32 ' \Docs & Settings\User\Local Settings\Temporary Internet Files
feUserCookies = 33 ' \Docs & Settings\User\Cookies
feUserDesktop = 16 ' \Docs & Settings\User\Desktop
feUserDocs = 5 ' \Docs & Settings\User\My Documents
feUserFavorites = 6 ' \Docs & Settings\User\Favorites
feUserHistory = 34 ' \Docs & Settings\User\Local Settings\History
feUserMusic = 13 ' \Docs & Settings\User\My Documents\My Music
feUserPics = 39 ' \Docs & Settings\User\My Documents\My Pictures
feUserRecent = 8 ' \Docs & Settings\User\Recent
feUserSendTo = 9 ' \Docs & Settings\User\SendTo
feUserStartMenu = 11 ' \Docs & Settings\User\Start Menu
feUserStartMenuPrograms = 2 ' \Docs & Settings\User\Start Menu\Programs
feUserStartup = 7 ' \Docs & Settings\User\Start Menu\Programs\Startup
feWindows = 36 ' \WINNT
feWindowsSystem = 37 ' C:\WINNT\System32
End Enum
Private Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Declare Function SHGetFolderPath Lib "shfolder" Alias "SHGetFolderPathA" (ByVal hwndOwner As Long, ByVal nFolder As Long, ByVal hToken As Long, ByVal dwFlags As Long, ByVal pszPath As String) As Long
Public Function SpecialFolder(pfe As FolderEnum) As String
Const MAX_PATH = 260
Dim strPath As String
Dim strBuffer As String
Dim lngHandle As Long
Dim lngLen As Long
strBuffer = Space$(MAX_PATH)
If pfe = feApp Then
lngHandle = GetModuleHandle(App.EXEName)
lngLen = GetModuleFileName(lngHandle, strBuffer, MAX_PATH)
strPath = Left$(strBuffer, lngLen)
strPath = Left$(strPath, InStrRev(strPath, "\") - 1)
If InStr(strPath, "Microsoft Visual Studio") > 0 Then strPath = App.Path
Else
If SHGetFolderPath(0, pfe, 0, 0, strBuffer) = 0 Then strPath = Left$(strBuffer, InStr(strBuffer, vbNullChar) - 1)
End If
If Right$(strPath, 1) = "\" Then strPath = Left$(strPath, Len(strPath) - 1)
SpecialFolder = strPath
End Function
Sample usage:
?SpecialFolder(feUserAppData)
C:\Documents and Settings\Ellis\Application Data
The weirdness in there dealing with the Application Path (feApp) is to more reliably handle when the exe is on a network share.
Re: [RESOLVED] Open a folder in %AppData%
Thanks Ellis,
This enumeration is a keeper. Just proves that there's moree than one way to skin a cat.