Click here to Skip to main content
15,115,503 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
protected void BtnBrowse_Click(object sender, EventArgs e)
        {
            //====== Create folder dialog box object
            FolderBrowserDialog objFolderDialog = new FolderBrowserDialog();
            //===== Pass object as Parameter and get Selected network folder
            TextBox1.Text  = GetNetworkFolders(objFolderDialog);
        }

        public static string GetNetworkFolders(FolderBrowserDialog FolderBrowserDialog)
        {
            //======= Get type of Folder Dialog bog
            Type type = FolderBrowserDialog.GetType();
            //======== Get Fieldinfo for rootfolder.
            System.Reflection.FieldInfo fieldInfo = type.GetField("rootFolder", BindingFlags.NonPublic | BindingFlags.Instance);
            //========= Now set the value for Folder Dialog using DirectCast
            //=== 18 = Network Neighborhood is the root
            fieldInfo.SetValue(FolderBrowserDialog, (Environment.SpecialFolder)18);
            //==== if user click on Ok, then it will return the selected folder.
            //===== otherwise return the blank string.
            if (FolderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                return FolderBrowserDialog.SelectedPath;
            }
            else
            {
                return "";
            }
        }
Posted
Updated 5-Oct-11 3:39am
v2
Comments
Nicholas Butler 5-Oct-11 8:37am
   
Why aren't you using the RootFolder property?
Mario Majčica 5-Oct-11 9:15am
   
Using Reflection is more pro! ;)
johannesnestler 13-Oct-11 9:25am
   
no - it is slow - this is not more pro... ;-)
Mario Majčica 13-Oct-11 9:28am
   
Check this out, maybe it helps ;)

http://en.wikipedia.org/wiki/Sarcastic
Bala Selvanayagam 5-Oct-11 10:09am
   
Just tried you code and it works for me.

above to browse a newwork path and its displayed in the text box...am i missing something in your question ?
BobJanova 5-Oct-11 11:45am
   
Define 'doesn't work'.
Mohammad A Rahman 6-Oct-11 19:21pm
   
Tested the code and it is working. Have debug the code, try with try..catch to find out if anything is happening there. :)
sagar_253 4-Aug-14 9:18am
   
Hey i have used above code to browse network, but when i click on Browse on win server 2008 machine by default "Make new folder" button gets enabled as rootnode is "Network". For other operating system its disabled and gets enabled when any folder from network machine node is selected.Do you have any idea why this is happeining

Suggest you tell us more here about exactly what you are trying to do: are you trying to select a computer on a network, and then select a folder within that selected computer ?

And what error message are you getting ?

See this CP article: "Folder Browser component for .NET By Rama Krishna Vavilala"[^].

If that doesn't work then see: which is based on Vavilala's CP article[^].

Are you using a version of .NET before 3.5; the reason I ask is I remember something on StackOverFlow about you would get an error trying to cast a number to an Environment.SpecialFolder if that were the case.

good luck, Bill
   
Hi,
Why dont you use fb.rootfolder property....it returns the root folder path....
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900