el archivo existe por nombre de archivo patrón


Estoy usando:

File.Exists(filepath)

Lo que me gustaría hacer es cambiar esto por un patrón, porque la primera parte del nombre del archivo cambia.

Por ejemplo: el archivo podría ser

01_peach.xml
02_peach.xml
03_peach.xml

¿Cómo puedo comprobar si el archivo existe basado en algún tipo de patrón de búsqueda?

Author: John Saunders, 2009-07-29

3 answers

Puede hacer una lista de directorios con un patrón para comprobar si hay archivos

string[] files = System.IO.Directory.GetFiles(path, "*_peach.xml", System.IO.SearchOption.TopDirectoryOnly);
if (files.Length > 0)
{
    //file exist
}
 105
Author: monkey_p,
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
2012-12-17 17:04:06

Si está utilizando. net framework 4 o superior, podría usar Directory.EnumerateFiles

bool exist = Directory.EnumerateFiles(path, "*_peach.xml").Any();

Esto podría ser más eficiente que usar Directory.GetFiles ya que evita iterar a través de toda la lista de archivos.

 52
Author: Claudio Redi,
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
2015-06-10 20:43:06
 5
Author: Mitch Wheat,
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
2017-05-23 10:31:12