Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Visual C# Question: Display Folder Contents On Form
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
rar




PostPosted: Wed Jun 30, 2010 9:56 am   Post subject: Visual C# Question: Display Folder Contents On Form

So evolving from my last question, I have another question about folder display.

How can I get the folder contents to display on the screen? I'm not sure which tool to use for this, but I'm sure I have to use the path name somehow...
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Wed Jun 30, 2010 6:36 pm   Post subject: Re: Visual C# Question: Display Folder Contents On Form

What do you mean by contents? Like in the folderDialog? Or just a list of files with their paths in a list? For the latter, Use Directory.

C#:

string source = "C:\\Blarg";
string[] dirEntries = Directory.GetDirectories(source);
foreach (string dirName in dirEntries)
    Systen.Console.WriteLine( System.IO.Path.GetFileName(dirName) );

string[] fileEntries = Directory.GetFiles(source);
foreach(string fileName in fileEntries)
    Systen.Console.WriteLine( System.IO.Path.GetFileName(fileName) );


Bear in mind, I'm also new to C# so there's could be other/better ways.
rar




PostPosted: Wed Jun 30, 2010 7:53 pm   Post subject: RE:Visual C# Question: Display Folder Contents On Form

code:
string[] dirEntries = Directory.GetDirectories(source);
            foreach (string dirName in dirEntries)
            {
                textBox1.Lines[count] = Path.GetFileName(dirName);
                if (count != dirEntries.Length - 1)
                {
                    count++;
                }
            }

Why am I running out of the array??? I don't know of any other way to print on different Lines in the textbox...
Zren




PostPosted: Wed Jun 30, 2010 9:33 pm   Post subject: RE:Visual C# Question: Display Folder Contents On Form

Both are string[]. I'll save ya some brainwaves.

textBox1.Lines = dirEntries;

As for your logic, I'm guessing it's because you didn't initialize textBox1.Lines' size meening it's still the length it was from the begining (empty) so there's your out of bounds error.

textBox1.Lines = new string[dirEntries.Length];
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: