TechyMedia

A .NET Technology blog | .NET,C# Progamming,C# tutorials,WPF WCF|Software Engineer

NAVIGATION - SEARCH

Browse Network using FolderBrowserDialog C# Control

.NET How to Browse Network using FolderBrowserDialog Control: Type folderBrowserType = folderBrowserDialog1.GetType();System.Reflection.FieldInfofieldInfo = folderBrowserType .GetField("rootFolder",BindingFlags.NonPublic | BindingFlags.Instance); fieldInfo .SetValue(folderBrowserDialog1, (Environment.SpecialFolder)18);

How to Browse Network using FolderBrowserDialog C# Control?

Many a times you may need to write programs which helps to access computers and devices that are on the network.That is, you need to give a provision to browse networks through the folderbrowserdialog control as below.

 

 

After dragging- dropping the control into the form setting Network (Network Neighbourhood ) as a root folder folder is not straight foward in VisualStudio FolderBrowserDialog control.In the property window of the FolderBrowserDialog control you cannot see Network as an option for Rootfolder property.

C++ Ultimate Video Training - Best Course

 

[Note :Networkshortcuts are not network itself]

This article explains a code snippet which you can use to make network as root folder in FolderBrowserDialog  control. This article also answers the following questions,

How to set Network folder as Rootfolder in FolderBrowser Dialog?

How to Browse only Network Folders using Folder Dialog Box Control ?

Code snippet to make network folder as root folder of Folderbrowserdialog is folderBrowserDialog1 is the control name.

Type folderBrowserType = folderBrowserDialog1.GetType();

System.Reflection.FieldInfo fieldInfo = folderBrowserType .GetField("rootFolder",

BindingFlags.NonPublic | BindingFlags.Instance);

fieldInfo .SetValue(folderBrowserDialog1, (Environment.SpecialFolder)18);

Here 18 stands for Network folder value in the SpecialFolder list enum.

 

See below a working sample 

Drag-drop the folderbrowserdialog control to your form.

Also add a button to the form. Write the btton click event as below

   

private void button1_Click(object sender, EventArgs e)

{

//Set Networ as your root folder

Type folderBrowserType = folderBrowserDialog1.GetType();

System.Reflection.FieldInfo fieldInfo = folderBrowserType.GetField("rootFolder",

BindingFlags.NonPublic | BindingFlags.Instance);

fieldInfo.SetValue(folderBrowserDialog1, (Environment.SpecialFolder)18);

 

//show the folder browser dialog 

DialogResultdlgresult = folderBrowserDialog1.ShowDialog();

if (dlgresult == DialogResult.OK)

            {

                 //Write your code logic 

                 MessageBox.Show("You are able to access  browse network using folderbrowserdialog control in dotnet");

            }            

        }

 

Browse Network using FolderBrowserDialog VB.NET Control - Code snippet

Dim folderBrowserType As Type = folderBrowserDialog1.[GetType]()

System.Reflection.FieldInfo fieldInfo = folderBrowserType.GetField("rootFolder", BindingFlags.NonPublic Or BindingFlags.Instance)

fieldInfo.SetValue(folderBrowserDialog1, DirectCast(18, Environment.SpecialFolder))

 

Happy Coding!

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading