Results 1 to 7 of 7

Thread: How can I search for a file?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Question How can I search for a file?

    hello!

    I have 4 strings which represent 4 file names...
    I want to search my computer for these strings, so if one of them is
    found, the search function will break and the full path of the file will be
    returned...

    how can I do that in C#?

    thanks!!

    Dekel C.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can I search for a file?

    Code:
    public string FindFile(string rootFolder, string fileName)
    {
        string filePath = System.IO.Path.Combine(rootFolder, fileName);
    
        // Check for the file in the current folder.
        if (System.IO.File.Exists(filePath))
            return filePath;
    
        // Check for the file in subfolders.
        foreach (string subfolder in System.IO.Directory.GetDirectories(rootFolder))
        {
            filePath = this.FindFile(subfolder, fileName);
    
            if (filePath != null)
                return filePath;
        }
    
        // The file was not found.
        return null;
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Re: How can I search for a file?

    thanks jmcilhinney!!

    now, if I want to scan the entire computer (starting with drive c, the d and
    so on...) what string should I put in the 'rootFolder' paramater?


    Dekel C.

  4. #4
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281

    Re: How can I search for a file?

    Put code somewhere to keep passing the drives (their paths) into the function.

    and to get all drives:
    System.IO.DriveInfo.GetDrives()

    Look it up in the object browser to see the return type, and you can even make it search 'just harddisks' (by DriveType)

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can I search for a file?

    Just be aware that DriveInfo is new for .NET 2.0. If you're using 1.x this option is not available to you. If that's the case then post back and we can supply an alternative.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281

    Re: How can I search for a file?

    Oh, ya.
    System.IO.Directory.GetLogicalDrives()
    would probably do the trick.

  7. #7
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: How can I search for a file?

    i posted something in the code bank a while back too...search for it there, might be useful, or not
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width