Usando Directorio.GetFiles con una expresión regular en C#?
Tengo este código:
string[] files = Directory.GetFiles(path, "......", SearchOption.AllDirectories)
Lo que quiero es devolver solo archivos que NO comiencen con p_
y t_
y tengan la extensión png o jpg o gif. ¿Cómo haría esto?
23
Author: altocumulus, 2011-12-09
3 answers
Directory.GetFiles
no soporta RegEx
de forma predeterminada, lo que puede hacer es filtrar por RegEx
en su lista de archivos. Echa un vistazo a este listado:
Regex reg = new Regex(@"^^(?!p_|t_).*");
var files = Directory.GetFiles(yourPath, "*.png; *.jpg; *.gif")
.Where(path => reg.IsMatch(path))
.ToList();
55
Author: Abdul Munim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-21 06:03:55
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-21 06:03:55
No puede pegar una expresión regular en el parámetro, es solo un filtro de cadena simple. Intente usar LINQ para filtrar después.
var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
.Where(s => s.EndsWith(".jpg") || s.EndsWith(".png"))
.Where(s => s.StartsWith("p_") == false && s.StartsWith("t_") == false)
6
Author: Ian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-09 09:40:52
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-09 09:40:52
Pruebe este código, también busca en cada unidad:
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
if (drive.RootDirectory.Exists)
{
DirectoryInfo darr = new DirectoryInfo(drive.RootDirectory.FullName);
DirectoryInfo[] ddarr = darr.GetDirectories();
foreach (DirectoryInfo dddarr in ddarr)
{
if (dddarr.Exists)
{
try
{
Regex regx = new Regex(@"^(?!p_|t_)");
FileInfo[] f = dddarr.GetFiles().Where(path => regx.IsMatch(path));
List<FileInfo> myFiles = new List<FileInfo>();
foreach (FileInfo ff in f)
{
if (ff.Extension == "*.png " || ff.Extension == "*.jpg")
{
myFiles.Add(ff);
Console.WriteLine("File: {0}", ff.FullName);
Console.WriteLine("FileType: {0}", ff.Extension);
}
}
}
catch
{
Console.WriteLine("File: {0}", "Denied");
}
}
}
}
}
2
Author: AnandMohanAwasthi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-17 10:07:10
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-17 10:07:10