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