在完整路径中搜索字符串c#

时间:2012-11-10 18:51:04

标签: c# string search

我有大量的MP3文件路径。我正在搜索这个数组,以找到整个路径中的任何内容来匹配搜索的内容。它只返回文件名中的匹配项,而不是路径。

老了:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test
{
    class Program
    {
          static void Main(string[] args)
          {
              _search("ant");
              Console.Read();
          }
          static void _search(string var)
          {
              string[] mp3 = new String[1];
              mp3 = Directory.GetFiles("c:\","*.mp3","SearchOption.AllDirectories);
              string[] temp = new string[1];
              int x = 0;
              for (int i = 0; i < mp3.Length; i++)
              {
                  if (mp3[i].Contains(var))
                  {
                      temp[x] = mp3[i];
                      x++;
                      Array.Resize(ref temp, x + 1);
                  }
              }
              _writeArray(temp);
          }
          static void _writeArray(string[] array)
          {
             for (int i = 0; i < array.Length; i++)
             Console.Write(array[i] + "\n");
          }
       }
    }

新:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test
{
     class Program
     {
          static void Main(string[] args)
          {
              List<string> search = searchSong("ant");
              foreach (string song in search)
              {
                  Console.WriteLine(song);
              }
           }

    static List<string> searchSong(string value)
    {
        value = value.ToLower();
        List<string> songs = new List<string>();
        String[] mp3 = null;
        mp3 = Directory.GetFiles(@"c:\users\owner\music\metal\sybreed", "*.mp3", SearchOption.AllDirectories).ToArray();
        foreach (string item in mp3)
        {
            string LowerCaseItem = item.ToLower(); 
            if (LowerCaseItem.Contains(value))
            {
              songs.Add(item);
            }
        }
        return songs;
    }
}

}

2 个答案:

答案 0 :(得分:2)

试试这段代码:

 class Program
    {
        static void Main(string[] args)
        {
            PrintItems(Search("John Lennon"));
            PrintFileName(Search("John Lennon"));
            Console.Read();
        }

        static string[] Search(string var)
        {
            return Directory.GetFiles(@"K:\mp3", "*" + var + "*.mp3", SearchOption.AllDirectories);
        }

        static void PrintItems(string[] array)
        {
            for (int i = 0; i < array.Length; i++)
                Console.Write(array[i] + "\n");
        }

        static void PrintFileName(string[] array)
        {
            foreach (var item in array)
            {
                FileInfo fi = new FileInfo(item);
                Console.WriteLine(fi.Name);
            }
        }
    }

答案 1 :(得分:1)

必须将路径作为参数搜索。

class program    
{

        static void Main(string[] args)
        {
            List<string> search = searchSong("SomeThing");
            foreach (string song in search)
            {
                Console.Writeline(song);
            }
        }

        public List<string> searchSong(string value)
        {
            List<string> songs = new List<string>();
            String[] mp3 = null;
            mp3 = Directory.GetFiles(@"D:\Songs", "*.mp3", SearchOption.AllDirectories).ToArray();
            foreach (String item in mp3)
            {
                if (item.Contains(value))
                {
                    songs.Add(item);
                }
            }
            return songs;
        }

    }