Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easiest way to get desktop directory 2

Status
Not open for further replies.

ESquared

Programmer
Dec 23, 2003
6,129
US
I have been using the windows API call GetOpenFileNameA to allow a user to browse for a file. You can set the starting directory of the dialog by specifying lpszInitialDir in the OPENFILENAME structure.

What's the easiest way to discover the user's desktop folder so I can pass this string in? Also, am I going to get the dialog navigated to the physical folder or to the Desktop-as-top-object item? I want the latter.

Passing %systemroot% as the string does work with GetOpenFileNameA to set to "C:\WINDOWS" or whatever. Is there an equivalent %desktopdir% or whatever? I tried "C:\..\.." but that didn't work.

A week or two ago I was about to use another group of functions where you set the starting folder using a callback function and a special kind "directory ID" whose name now escapes me (almost like an hWnd for directories instead of windows). If I cannot set the default directory with the above method (which I switched to because it was simpler) can anyone point me to the names of these functions? Once I have the name I'll be set with MSDN reference and online searches.

For what it's worth I'm programming in VB6. Thank you so much!

-E
 
Take a look at the ShellSpecialFolder Api a quick google or MSDN search should turn up the Const for the desktop.
 
Do you mean SHGetSpecialFolderLocation?

I get just 14 hits on ShellSpecialFolder, and for that I see references to the Shell object. I suppose I can use that although I was expecting to do an API call.

 
Sorry had a few cocktails this evening and was shooting from the top of my head, let's try this.

Option Explicit

Private Type SHORTITEMID
cb As Long
abID As Integer
End Type
Private Type ITEMIDLIST
mkid As SHORTITEMID
End Type
Private Declare Function SHGetPathFromIDList Lib _
"shell32.dll" (ByVal pidl As Long, _
ByVal pszPath As String) As Long
Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
Private Const CSIDL_DESKTOP = &H0

Public Function fGetSpecialFolder(ByVal lngCSIDL As Long) As String
Dim udtIDL As ITEMIDLIST
Dim lngRtn As Long
Dim strFolder As String
lngRtn = SHGetSpecialFolderLocation(Me.hWnd, lngCSIDL, udtIDL)
If lngRtn = 0 Then
strFolder = Space$(260)
lngRtn = SHGetPathFromIDList( _
ByVal udtIDL.mkid.cb, ByVal strFolder)
If lngRtn Then
fGetSpecialFolder = Left$(strFolder, _
InStr(1, strFolder, Chr$(0)) - 1)
End If
End If

End Function
Private Sub Command1_Click()
MsgBox fGetSpecialFolder(CSIDL_DESKTOP)
End Sub
 
If you are using VB, then you could also consider using Microsoft's Shell Controls and Automation library (as illustrated in thread222-740362)
 
What's the *best* way to do it? When multiple methods present themselves, how do I decide? Do I base my decision simply on what is quicker and easier to program? What is faster execution, or more robust, or... whatever?
 
In my opinion, quicker & easier way is ActiveX or Automation library. Faster execution is API.

More about it, Win32 API always installed along with windows installation. ActiveX sometimes belongs to the languange development itself, so you have to distribute it along with the application. And things like Windows Scripting Object there are many users doesn't want it exist in their computer.

So if you know how to do it with API, go with API. But if you have trouble with API or you don't have a enough time to develop and dig up the API function then go with ActiveX.

Just my 0.02$ :)
Regards

-- AirCon --
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top