• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 422
  • Last Modified:

API to show folder dialog with ONLY Network Folders or ONLY Local folders

Hello !

I need to provide user with two dialog boxes. First has ONLY local (i.e hide all network folders) and second - ONLY network folders.
I use VB6 + SP2, target OS is Win XP. I want pure API solutions that work OK for all Win XP (and if possible Win 2000 + SP4)
0
dvplayltd
Asked:
dvplayltd
1 Solution
 
gecko_au2003Commented:
go to http:\\vbnet.mvps.org or www.allapi.net and search around on there, they have great examples with coding to show you a bunch of things :)
0
 
dvplayltdAuthor Commented:
I prefer to use  API SHBrowseForFolder, however I can NOT find flags that will limit folders to network or local.
0
 
rdwillettCommented:
Will take a jab at this and you can change it however  u want


Add Three text boxes named Text1 and Text2 and Text3
Add Three command buttons named Command1 and Command2 and Command3
Command1 returns only a valid network server or workstation including shares
Command2 returns all drives
Command3 only returns local drives
Add the following code to form and run project

Also refer to this website: http://vbnet.mvps.org/index.html?code/browse/browseoverview.htm

Option Explicit
Private Const ERROR_SUCCESS As Long = 0
Private Const MAX_PATH As Long = 260
Private Const CSIDL_NETWORK As Long = &H12
Private Const BIF_RETURNONLYFSDIRS As Long = &H1
Private Const BIF_BROWSEFORCOMPUTER As Long = &H1000
Private Const BIF_DONTGOBELOWDOMAIN As Long = &H2
Private Const CSIDL_DRIVES As Long = &H11


Private Type BROWSEINFO
   hOwner As Long
   pidlRoot As Long
   pszDisplayName As String
   lpszTitle As String
   ulFlags As Long
   lpfn As Long
   lParam As Long
   iImage As Long
End Type
 
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
   Alias "SHGetPathFromIDListA" _
  (ByVal pidl As Long, _
   ByVal pszPath As String) As Long
 
Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
   Alias "SHBrowseForFolderA" _
  (lpBrowseInfo As BROWSEINFO) As Long
 
Private Declare Function SHGetSpecialFolderLocation _
   Lib "shell32.dll" _
  (ByVal hwndOwner As Long, _
   ByVal nFolder As Long, _
   pidl As Long) As Long
   
Private Declare Sub CoTaskMemFree Lib "ole32.dll" _
   (ByVal pv As Long)
Private Sub Command1_Click()
   Text1.Text = GetBrowseNetworkWorkstation()
End Sub
Private Sub Command2_Click()
   Text2.Text = GetBrowseNetworkShare()
End Sub
Private Sub Command3_Click()
   Text3.Text = GetLocal()
End Sub
Private Function GetBrowseNetworkShare() As String
    'returns only a valid share on a network server or workstation
    Dim BI As BROWSEINFO
    Dim pidl As Long
    Dim sPath As String
    Dim pos As Integer
 
    If SHGetSpecialFolderLocation(Me.hWnd, _
                                 CSIDL_NETWORK, _
                                 pidl) = ERROR_SUCCESS Then
       
        With BI
            .hOwner = Me.hWnd
            .pidlRoot = pidl
            .pszDisplayName = Space$(MAX_PATH)
            .lpszTitle = "Select a network computer or share."
            .ulFlags = BIF_RETURNONLYFSDIRS
        End With
         
        pidl = SHBrowseForFolder(BI)
        If pidl <> 0 Then
            sPath = Space$(MAX_PATH)
            If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then
                pos = InStr(sPath, Chr$(0))
                GetBrowseNetworkShare = Left$(sPath, pos - 1)
            End If
            Call CoTaskMemFree(pidl)
        Else:
            GetBrowseNetworkShare = "\" & BI.pszDisplayName
        End If
    End If
End Function
Private Function GetMyComputer() As String
    'returns all computer drives
    Dim BI As BROWSEINFO
    Dim pidl As Long
    Dim sPath As String
    Dim pos As Integer
   
    If SHGetSpecialFolderLocation(Me.hWnd, _
                                 BIF_BROWSEFORCOMPUTER, _
                                 pidl) = ERROR_SUCCESS Then
       
       
        With BI
            .hOwner = Me.hWnd
            .pidlRoot = pidl
            .pszDisplayName = Space$(MAX_PATH)
            .lpszTitle = "Select a network computer."
            .ulFlags = BIF_BROWSEFORCOMPUTER
        End With
         

        If SHBrowseForFolder(BI) <> 0 Then
            GetMyComputer = "\" & BI.pszDisplayName
        End If
        Call CoTaskMemFree(pidl)
    End If
End Function

Private Function GetLocal() As String
    'returns all local computer drives
    Dim BI As BROWSEINFO
    Dim pidl As Long
    Dim sPath As String
    Dim pos As Integer
   
    If SHGetSpecialFolderLocation(Me.hWnd, _
                                 CSIDL_DRIVES, _
                                 pidl) = ERROR_SUCCESS Then
       
        With BI
            .hOwner = Me.hWnd
            .pidlRoot = pidl
            .pszDisplayName = Space$(MAX_PATH)
            .lpszTitle = "Select a network computer."
            .ulFlags = BIF_BROWSEFORCOMPUTER
        End With
         
        If SHBrowseForFolder(BI) <> 0 Then
            GetLocal = "\" & BI.pszDisplayName
        End If
        Call CoTaskMemFree(pidl)
    End If
End Function

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now