search function for directories

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • michaeldebruin
    New Member
    • Feb 2011
    • 134

    search function for directories

    I am trying to give my program a search function which can search for directories. At the moment it can search for directories but it's only searching for the directories who have exactly the name which is put in the textbox. And I want a function that can search for every directory which has a the name, standing in the textbox. For example if I typ in the word "test" and I have 2 directories, one with the name test 1 and the other one test 2. It will have to display both directories.
    If anyone knows how to create a search function like that, please tell me. I would appreciate some codes.

    Many thanks in advance.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    They support wild cards. Try something like this:
    string[] MatchingDirecto ries = Directory.GetDi rectories(BaseP ath, "*" + SearchField + "*", SearchOption.Al lDirectories);

    Comment

    • michaeldebruin
      New Member
      • Feb 2011
      • 134

      #3
      Thanks, but now I am getting the problem that it shows as result "string[]-matrix" and not the real and full name of the directories it found.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well yes, you are getting an array of strings (that is a string[])
        With each entry being one of the possible directories.

        Comment

        • michaeldebruin
          New Member
          • Feb 2011
          • 134

          #5
          And it is good that I am getting an array of strings. But I need an search function which returns all the real and full names of all the possible directories. So if you know some codes which should be able to do the work, please tell me.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            What do you mean by real and full names?
            Each entry in that string[] should be like:
            c:\temp\files\t est 1\
            c:\temp\files\t est 2\
            c:\temp\files\f ranks test\

            EDIT:
            Now I suppose it will ALSO give you entries with your search term NOT being the lowest directory, as long as its below the basepath:
            c:\temp\files\t est 1\
            c:\temp\files\t est 2\
            c:\temp\files\f ranks test\
            c:\temp\files\t est 2\somename\
            c:\temp\files\f ranks test\try2\
            Last edited by Plater; Apr 27 '11, 04:58 PM.

            Comment

            • michaeldebruin
              New Member
              • Feb 2011
              • 134

              #7
              what I mean is:
              if a directory name is franks test and I say search for the test. The program has to show the full directory name as in this case franks test.
              And what you say each entry in that string[] should be like:
              c:\temp\files\t est 1\.
              But the only thing my program display is "string[]-matrix".

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Are you just doing MatchingDirecto ries.ToString() by anychance?
                Try
                [code=C#]
                foreach (string singleentry in MatchingDirecto ries)
                {
                //do something with the single entry
                }
                [/code]

                Comment

                • michaeldebruin
                  New Member
                  • Feb 2011
                  • 134

                  #9
                  the code I have at the moment is:
                  Code:
                  string[] MD = Directory.GetDirectories(Environment.CurrentDirectory, searchtext.Text , SearchOption.AllDirectories);
                  
                              if (MD != null)
                              {
                                  result.Items.Add(MD);
                                  searchtext.Clear();
                              }
                  Last edited by Plater; May 3 '11, 12:36 PM.

                  Comment

                  Working...