1

我有大量 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;
    }
}

}

4

2 回答 2

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);
            }
        }
    }
于 2012-11-10T19:03:26.173 回答
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;
        }

    }
于 2012-11-10T19:22:05.020 回答