2017-10-09 1 views
2

Comment puis-je chercher un chemin comme ça en C#:C# Répertoire-SEARCHPATTERN Subdirectorie (s)

"C: \ MyApp \ * \ log"

Je veux tous les répertoires qui correspond cette recherche modèle.

Exemple de résultat:

C: \ MyApp \ 20171009 \ log
C: \ MyApp \ 20171008 \ log
C: \ MyApp \ 20171007 \ log

En Powershell il fonctionne avec get-article

+0

Il faudrait mettre en œuvre un tel algorithme vous – DiskJunky

+0

Vous pouvez essayer d'exécuter la commande PowerShell en utilisant 'RunspaceInvoke' – Fruchtzwerg

Répondre

0

J'ai trouvé un solution pour mon problème.

Je l'ai modifié pour l'utilisation de l'annuaire.

public static List<string> GetAllMatchingPaths(string pattern) 
     { 
      char separator = Path.DirectorySeparatorChar; 
      string[] parts = pattern.Split(separator); 

      if (parts[0].Contains('*') || parts[0].Contains('?')) 
       throw new ArgumentException("path root must not have a wildcard", nameof(parts)); 

      return GetAllMatchingPathsInternal(String.Join(separator.ToString(), parts.Skip(1)), parts[0]); 
     } 

     private static List<string> GetAllMatchingPathsInternal(string pattern, string root) 
     { 
      char separator = Path.DirectorySeparatorChar; 
      string[] parts = pattern.Split(separator); 

      for (int i = 0; i < parts.Length; i++) 
      { 
       // if this part of the path is a wildcard that needs expanding 
       if (parts[i].Contains('*') || parts[i].Contains('?')) 
       { 
        // create an absolute path up to the current wildcard and check if it exists 
        var combined = root + separator + String.Join(separator.ToString(), parts.Take(i)); 
        if (!Directory.Exists(combined)) 
         return new List<string>(); 

        if (i == parts.Length - 1) // if this is the end of the path (a file name) 
        { 
         return (List<string>) Directory.EnumerateFiles(combined, parts[i], SearchOption.TopDirectoryOnly); 
        } 
        else // if this is in the middle of the path (a directory name) 
        { 
         var directories = Directory.EnumerateDirectories(combined, parts[i], SearchOption.TopDirectoryOnly); 

         List<string> pts = new List<string>(); 
         foreach (string directory in directories) 
         { 
          foreach (string item in GetAllMatchingPathsInternal(String.Join(separator.ToString(), parts.Skip(i + 1)), directory)) 
          { 

           pts.Add(item); 
          } 

         } 

         return pts; 
        } 
       } 
      } 
+0

étiez-vous pas en mesure d'obtenir la liste des dir avec la réponse que je fournis? – Dhejo

+0

cela n'a pas fonctionné avec mon exemple du haut. –

2

Essayez cette fonction de fichier à base itérateur:

var path = @"C:\temp"; 
foreach (var file in Directory.EnumerateFiles(path, "*.log", SearchOption.AllDirectories)) 
{ 
    Console.WriteLine(file); 
} 

Pour plus d'informations montrent here

0

Si vous essayez d'obtenir seulement les répertoires avec Nom du journal qui correspondent au modèle C: \ MyApp * \ log, le code suivant doit aider:

var dirs = Directory.EnumerateDirectories(@"C:\Temp\","log", SearchOption.AllDirectories); 

Notez que modèle de recherche est le nom du répertoire et pas de nom de fichier ou d'extension de fichier