Click here to Skip to main content
15,879,184 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 2: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 ?

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
 
Share this answer
 
Hi,
Why dont you use fb.rootfolder property....it returns the root folder path....
 
Share this answer
 

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



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