I'm creating a compression program for one of my classes and I'm getting stumped on one aspect of it which is more cosmetic than anything but I'm going for gold here I suppose.

Anyway I have a windows form that has a textbox with a button that executes FolderBrowserDiaglog (which works fine) and then places the directory you select into the textbox field (also works).

Once thats done it sets the current directory to the one you selected (also works).

Now where I'm stumped for some reason is I have a listbox at the bottom that I want to now fill up with all the files within the folder I selected. I'm trying to use OpenFileDialog but it opens a window for me to select a file (I want that to be done within the listbox).

Any ideas or suggestions really??

Recommended Answers

All 4 Replies

Ok I used the directory.getfiles and got them to show up but when they do it shows the path & filename, how do I go about having it ignore the directory so that only the filename is listed?

The reason is when I select an entry or multiple entries to compress it complains because it's trying to compress it as a file (directory name and all).

Path.GetFileName

I tried that one but it also ended up listing the directory structure as well, what I ended up using was FileInfo and running a foreach to propagate the checked listbox, that gave me what I needed and got the compression function to work as I needed (more or less), only flaw I have now is making it understand when I check off multiple files I want all those checked to be compressed, but I think I have an idea of what I'm missing for that.

Anyway here is the code snippet that I ended up using (in case anyone else happens to for some odd reason what to do what I wanted).


Here is the code snippet for browsing through the directories on the Hard drive:

private void button1_Click(object sender, EventArgs e)
          {
               //create instance of folder browser to navigate to desired folder to compress files
               DialogResult result = fBrowser.ShowDialog();
               
               //process this if user clicks OK button
               if (result == DialogResult.OK)
               {    
                    //string strPath stores chosen path
                    strPath = fBrowser.SelectedPath;

                    //put that path in the textbox1
                    textBox1.Text = strPath;
               }

               //set current directory to be the one you navigated to 
               //(this is also the folder that will store the compressed file)
               Directory.SetCurrentDirectory(strPath);
          }

And here is the code to add the files in that directory to my checkedlistbox

//process functions when button3 is clicked
          private void button3_Click(object sender, EventArgs e)
          {
               //get contents of directory stored in "strPath"
               DirectoryInfo di = new DirectoryInfo(strPath);

               //create array that holds requested files from folder stored in "di" variable
               FileInfo[] rgFiles = di.GetFiles("*.*");

               //move through FileInfo array and store in new array of fi
               foreach (FileInfo fi in rgFiles)
               {
                    checkedListBox1.Items.Add(fi.Name); //add files located into listbox
               }

          }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.