-1

i needed to filter out the whole computer and find only .sto files. I want theses files displayed in a listbox. I am needing them displayed like this: "text.sto". I am using Visual C# Express. How do i do that?

Hunter Mitchell
  • 7,063
  • 18
  • 69
  • 116
  • 8
    What did you try so far? – Vlad May 16 '12 at 16:49
  • well, i have researched and researched, but can not find a way to do it, that is why i came here... – Hunter Mitchell May 16 '12 at 16:50
  • @Elite: which part of the task makes a problem? Scanning the filesystem? Filtering the results? Creating the UI? – Vlad May 16 '12 at 16:51
  • 1
    Your question is really broad. You're pretty much asking for a whole app. You need to break down into parts, try each one and then come back for help. – Paul Sasik May 16 '12 at 16:51
  • I am sorry, i am young and am just learning, please do not be harsh... – Hunter Mitchell May 16 '12 at 16:51
  • well, the scanning and filtering – Hunter Mitchell May 16 '12 at 16:51
  • 1
    You can get the drive list using [DriveInfo.GetDrives](http://msdn.microsoft.com/en-us/library/system.io.driveinfo.getdrives.aspx). Then, you can scan each drive with [Directory.EnumerateFile](http://msdn.microsoft.com/en-us/library/dd383571.aspx) (this will take care of filtering, too). – Vlad May 16 '12 at 16:55
  • @Paul: actually, C#'s Directory.EnumerateFiles can take care of recursion itself. – Vlad May 16 '12 at 16:56
  • 2
    @EliteGamer they're not being harsh, they're being helpful. Research is just as important as writing the code. Google is the best tool for that research - use and abuse it. – Yatrix May 16 '12 at 17:00

2 Answers2

2

Make sure you have this using directive at the top of your code file:

using System.IO;

Get the directories with this method:

string[] dirs = Directory.GetFiles(@"c:\", "*.sto", SearchOption.AllDirectories);

It takes a second parameter that allows you to filter the results. More information on the method can be found here: http://msdn.microsoft.com/en-us/library/ms143316.aspx

Incase you do not want to use the built in .Net function for this (because it can throw access errors and provide no results), here is a answer to the question that shows doing it a different way: https://stackoverflow.com/a/929418/184746

Referencing from the aforementioned article, here is a method that will catch and wont give you errors with filtering added for *.sto:

public static IEnumerable<string> GetFiles(string path)
{
    Queue<string> queue = new Queue<string>();
    queue.Enqueue(path);
    while (queue.Count > 0)
    {
        path = queue.Dequeue();
        try
        {
            foreach (string subDir in Directory.GetDirectories(path))
            {
                queue.Enqueue(subDir);
            }
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine(ex);
        }
        string[] files = null;
        try
        {
            files = (from f in Directory.GetFiles(path)
                     where f.EndsWith(".sto")
                     select f).ToArray();
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine(ex);
        }
        if (files != null)
        {
            foreach (string t in files)
            {
                yield return t;
            }
        }
    }
}

You can use it like this:

string[] dirs = GetFiles(@"c:\").ToArray();

After that, create a listbox in the designer, and iterate through and add the items

foreach(var f in dirs)
{
    listBox1.Items.Add(String.Format("\"{0}\"", f));
}

Out of curiosity, what was your search parameter when researching this? Because a simple google search reveals all you need: "get list of files C#"https://www.google.ca/search?sourceid=chrome&ie=UTF-8&q=get+list+of+files+C%23

Community
  • 1
  • 1
caesay
  • 16,932
  • 15
  • 95
  • 160
0

One solution to search for sto files but I cant say how fast it'll be

string[] filePaths = Directory.GetFiles(@"c:\", 
                         "*.sto", 
                         SearchOption.AllDirectories);
Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99