Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

How to Browse Network Folders using Folder Dialog Box Control ?

4.82/5 (16 votes)
18 Sep 2007CPOL1 min read 1   4.3K  
An article on how to browse network Folders using VB.NET using Folder Dialog box Control
Screenshot - app_UI.jpg

Screenshot - Browse_Folders.jpg

Introduction

In the Folder Browse dialog box, we can browse our required folder except Network Neighborhood as a root folder. To browse the Network folders using Folder Dialog box control is quite difficult. I have seen lots of solutions on the Internet and found several lines of code to achieve this thing. But here, I am showing you a few lines code to achieve the same thing.

Background

I was developing one distributed application and my requirement was to browse only the
Network folders. I searched the Internet for this problem and found that the folder dialog
box control does not directly support Network folders browsing like Desktop folder or
other folders. This article will help you to do this thing in an easy manner.

Using the Code

The code that I have written to browse Network folders is quite easy to understand. Here is the code.

VB.NET
Private Sub btnBrowse_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnBrowse.Click

    '====== Create folder dialog box object
    Dim objFolderDialog As New FolderBrowserDialog()
    '===== Pass object as Parameter and get Selected network folder
    txtPath.Text = GetNetworkFolders(objFolderDialog)
End Sub

''' <summary>
''' This function will get the Folderdialog as parameter and return the 
''' Selected
''' Folders UNC path. 
''' Ex: <a href="%22%22file://server1/TestFolder'''%22%22">\\Server1\TestFolder
'''</a> </summary>
''' <param name="oFolderBrowserDialog"></param>
''' <returns>it will return the Selected Folders UNC Path</returns>
''' <remarks></remarks>

Public Shared Function GetNetworkFolders(ByVal oFolderBrowserDialog _
      As FolderBrowserDialog) As String
    '======= Get type of Folder Dialog bog
    Dim type As Type = oFolderBrowserDialog.[GetType]
    '======== Get Fieldinfo for rootfolder.
    Dim fieldInfo As Reflection.FieldInfo = type.GetField("rootFolder", _
    BindingFlags.NonPublic Or BindingFlags.Instance)
    '========= Now set the value for Folder Dialog using DirectCast
    '=== 18 = Network Neighborhood is the root
    fieldInfo.SetValue(oFolderBrowserDialog,_
                      DirectCast(18, Environment.SpecialFolder))
    '==== if user click on Ok, then it will return the selected folder.
    '===== otherwise return the blank string.
    If oFolderBrowserDialog.ShowDialog() = DialogResult.OK Then
        Return oFolderBrowserDialog.SelectedPath
    Else
        Return ""
    End If
End Function

fieldInfo.SetValue(oFolderBrowserDialog, DirectCast(18, Environment.SpecialFolder))here you can also set other folder browsers constant.

Here is a list of constants:

Constant Description
0 Desktop folder
1 Internet Explorer
2 Programs folder of the Start menu
3 Control Panel folder
4 Printers folder
5 Documents folder of the Start menu
6 Favorites folder of the Start menu
7 Startup folder of the Start menu
8 Recent folder
9 SendTo folder
10 Recycle Bin folder
11 Start menu folder
16 Desktop (physical) folder
17 My Computer
18 Network Neighborhood
19 Nethood folder
20 Fonts folder
21 Templates folder

Summary

In this way, you can browse Network folders. Let me know if anyone has a problem while using the attached code.

History

  • 18th September, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)