Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 118,309 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,685 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



POPULATING SUBDIRECTORIES IN LISTBOX

 
Reply to this topicStart new topic

POPULATING SUBDIRECTORIES IN LISTBOX, Pls help me

jee
post 26 Apr, 2008 - 06:42 PM
Post #1


New D.I.C Head

*
Joined: 20 Apr, 2008
Posts: 10

Hi Frnds!!!!
pls help me i m in need of a code...

first i want to describe my need...
in C#(WPF) i want a combobox and a listbox in which i need all logical drives to be populated in tha combobox and its directory should be populated in the listbox when a drive is selected..
here i wrote the code for populating drives in combobox and i got the output and the code is given below but i cant get the code for getting its subdirectory in listbox...
CODE

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                foreach (string logicalDrive in Environment.GetLogicalDrives())
                {
                    comboBox1.Items.Add(logicalDrive);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


i have done this same application in VB.NET and got the output.
the vb coding for populating the subdirectories in listbox is as follows.
CODE

Public Sub AddDirectory(ByVal subdir1 As String)
        For Each subdir As String In System.IO.Directory.GetDirectories(subdir1)
            'add the directory to the Listbox
            Me.ListBox1.Items.Add(subdir)

            Try
                'check if there are more sub directories
                If System.IO.Directory.GetDirectories(subdir).Length <> 0 Then
                    AddDirectory(subdir)
                End If
            Catch ex As Exception

            End Try
        Next
    End Sub

i cant find a way to convert it to C# coding..(for populating the sub directories in listbox)
Pls frnds help me out to overcome my problem and give a favourable reply..
thanks in advance....

This post has been edited by jee: 27 Apr, 2008 - 01:23 PM
User is offlineProfile CardPM

Go to the top of the page


gbeltrao
post 5 Aug, 2008 - 11:31 AM
Post #2


New D.I.C Head

*
Joined: 28 Jul, 2008
Posts: 1


My Contributions


QUOTE(jee @ 26 Apr, 2008 - 06:42 PM) *

Hi Frnds!!!!
pls help me i m in need of a code...

first i want to describe my need...
in C#(WPF) i want a combobox and a listbox in which i need all logical drives to be populated in tha combobox and its directory should be populated in the listbox when a drive is selected..
here i wrote the code for populating drives in combobox and i got the output and the code is given below but i cant get the code for getting its subdirectory in listbox...
CODE

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                foreach (string logicalDrive in Environment.GetLogicalDrives())
                {
                    comboBox1.Items.Add(logicalDrive);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


i have done this same application in VB.NET and got the output.
the vb coding for populating the subdirectories in listbox is as follows.
CODE

Public Sub AddDirectory(ByVal subdir1 As String)
        For Each subdir As String In System.IO.Directory.GetDirectories(subdir1)
            'add the directory to the Listbox
            Me.ListBox1.Items.Add(subdir)

            Try
                'check if there are more sub directories
                If System.IO.Directory.GetDirectories(subdir).Length <> 0 Then
                    AddDirectory(subdir)
                End If
            Catch ex As Exception

            End Try
        Next
    End Sub

i cant find a way to convert it to C# coding..(for populating the sub directories in listbox)
Pls frnds help me out to overcome my problem and give a favourable reply..
thanks in advance....

Hello Jee,
Here's the code translated:
CODE

public void AddDirectory(string subdir1)
{
     foreach (string subdir in System.IO.Directory.GetDirectories(subdir1)) {
         //add the directory to the Listbox
         this.ListBox1.Items.Add(subdir);
        
         try {
             //check if there are more sub directories
             if (System.IO.Directory.GetDirectories(subdir).Length != 0) {
                 AddDirectory(subdir);
             }
         }
         catch (Exception ex) {
            
         }
     }
}


And here's a free tool to translate your VB.NET code to C#:

VB.NET to C#

Regards,
Gilberto

User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/10/08 11:42AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month