Is there a way to use the common dialog control just to get a folder's application path, I also need to create new folders in the dialog box.
Thanks in advance.
Printable View
Is there a way to use the common dialog control just to get a folder's application path, I also need to create new folders in the dialog box.
Thanks in advance.
What's a folder's application path?
Sorry I mean of course if I click on a folder I want to return the folders name, when you use commendialog you can create a new folder but you can only receive the name on a file not on a folder.
do you want to get the name and path of the "folder" you selected using commondialog or the name of the "file"??
Harsh Gupta
I want to get the name and path of the "folder" I selected, but I also need to be able to create a new one from the dialog box.
using common dialog, it is too complicated because you cannot select a folder through it. but you can use it by selecting one file inside that folder:
common dialog has in-built "Create New Folder" Button next to "View Desktop" button.VB Code:
Private Sub Command1_Click() Dim iPath$ cd.ShowOpen iPath = Mid$(cd.FileName, 1, InStrRev(cd.FileName, "\")) 'Path and Name of folder MsgBox "Path of the folder is: " & iPath MsgBox "Name of the folder is: " & Split(iPath, "\")(UBound(Split(iPath, "\")) - 1) 'Path and name of file MsgBox "Path of the file is: " & cd.FileName MsgBox "Name of the file is: " & cd.FileTitle End Sub
Alternative method - recommended
use BrowseforFolder API, it will help you in selecting only folder, and return the path of the folder. (just a bit of coding and you may get the name of the folder yourself, not difficult):
to add a New folder, if you are using XP, then it would be not much problem. please take a look at this post.VB Code:
Private Type BrowseInfo hWndOwner As Long pIDLRoot As Long pszDisplayName As Long lpszTitle As Long ulFlags As Long lpfnCallback As Long lParam As Long iImage As Long End Type Const BIF_RETURNONLYFSDIRS = 1 Const MAX_PATH = 260 Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long) Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long Private Sub Form_Load() 'KPD-Team 1998 'URL: [url]http://www.allapi.net/[/url] Dim iNull As Integer, lpIDList As Long, lResult As Long Dim sPath As String, udtBI As BrowseInfo With udtBI 'Set the owner window .hWndOwner = Me.hWnd 'lstrcat appends the two strings and returns the memory address .lpszTitle = lstrcat("C:\", "") 'Return only if the user selected a directory .ulFlags = BIF_RETURNONLYFSDIRS End With 'Show the 'Browse for folder' dialog lpIDList = SHBrowseForFolder(udtBI) If lpIDList Then sPath = String$(MAX_PATH, 0) 'Get the path from the IDList SHGetPathFromIDList lpIDList, sPath 'free the block of memory CoTaskMemFree lpIDList iNull = InStr(sPath, vbNullChar) If iNull Then sPath = Left$(sPath, iNull - 1) End If End If MsgBox sPath End Sub
you may notice that this also a new method of doing it.
of you are using Win 98, then it is very tricky, but take look at this thread.
hope it helps you.
Harsh Gupta
Thank you, just what I look for.