Jump to content

pulling a list of files in C#

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hello I am back!!

Just thought i wait, finally i decided to buy a book on C# (book is now being shipped to me bought it ebay, author wrox books), studying a lot in SQL so i can put the two together as a career and later go into C++ to have 3 languages under my belt, anyway on with the question:

i want to make a program with check boxes to select which type of files you want to find in a certain directly (sorta like windows search bar) except you can search it by type and name in a wild search and click on the search button and come up with a list below in a results box from a listbox etc, how can this be done in Visual studio 2008 C#, i made a small nice gui interface but of course me being only 1 month experience in C# i dont know squat, but of course if you can show me just a small sample that would help a lot, i got down the browse button down for anyones reference if they want to know how if you want to click on browse which is in the code below however how about what i am requesting?

 OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "C# Corner Open File Dialog";
            fdlg.InitialDirectory = @"c:\";
            fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = fdlg.FileName;
            }

Edited by Jordan, 22 July 2008 - 08:57 AM.


#2
VCKicks

VCKicks

    Newbie

  • Members
  • PipPip
  • 24 posts
greetings,

Okay what you want to do is use the System.IO namespace (add it at the very top).

Then use the Directory class to get a list of filename inside a specific directory, Also you can get the directories inside that directory and check for files in there. That would take recursive processing of folders.

However in a nutshell, using Directory.GetFiles() which will return an array of strings, each of which is a filename. Then check the extension of each filename and if it matches one of the checkboxes add it to the listbox.

use the Path class if you want to get certain parts of the filename (name, extension, etc)

hope that helps
Visual C# Kicks - Everything C#.NET programming related

#3
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Great Thanks, but how do you search for certain files such as .txt files only or .doc files etc when the user presses search button?

#4
VCKicks

VCKicks

    Newbie

  • Members
  • PipPip
  • 24 posts
okay so for example let's say you're searching folder "C:\MyFolder"

so you get the list of files like such:

string[] fileList = Directory.GetFiles("C:\MyFolder");

Then go through each filename, and if it ends with the extension you're looking for, then add it to the list

foreach (string myFile in fileList)

{

   if (myFile.EndsWith(".txt") || myFile.EndsWith(".doc"))

          myListBox.Items.Add(myFile);

}

I didn't test out the code but that should work. In that example it would only show files that are either txt or doc.

hope that clears it up a bit
Visual C# Kicks - Everything C#.NET programming related

#5
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
it does a lot thank you

#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Well done, VCKicks, excellent work! +rep given.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#7
VCKicks

VCKicks

    Newbie

  • Members
  • PipPip
  • 24 posts
thanks, glad to help
Visual C# Kicks - Everything C#.NET programming related

#8
k3rn31

k3rn31

    Newbie

  • Members
  • Pip
  • 4 posts
thank you