Return a similar value from path

2

No result in this post , I would like to know how it would be used the Directory.GetFiles function, to list the files and folders within it without showing the full path.

Ex:

public List Listar(String a){
    return Directory.GetFiles(a, "* .*").ToList();
}

static void Main(string[] args){ var lista = Listar(@"C:\Windows\Inf"); foreach(string a in lista){ Console.WriteLine(a); } Console.ReadLine(); Console.Exit(); }

  

In this context you should return instead of C: \ Windows \ Inf \ .inf would only return the file: \ .inf file the same would be with the folders.

     
    

Ex: There is a folder with the name of files0 it would return instead of C: \ Windows \ Inf \ files0 \ strong>

  

OBS: I was told the following: Run:

var arquivos = Directory.EnumerateFiles("C:\Windows\Inf", "*",
                   SearchOption.AllDirectories).Select(Path.GetFileName);
  

But I want to run this code and Visual C # 2008/2010 says that the reference: Directory.EnumerateFiles does not exist because I only use version 4.0 of the Microsoft .NET Framework

    
asked by anonymous 13.10.2015 / 22:38

2 answers

4

First, you could not use Directory.EnumerateFiles() because it should not have imported the System.IO namespace because it works in the .NET Framework 4.0.

This solution takes all files within a given folder and within all subfolders, showing only the file name on the console. If you want to get only from the parent directory, change the third parameter of Directory.EnumerateFiles() to SearchOption.AllDirectories to SearchOption.TopDirectoryOnly .

There are other ways to do this, but I think this one already solves your problem in a very simple and practical way.

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO; //Importe este namespace para usar Directory.EnumerateFiles()

namespace TesteArquivos
{
    class Program
    {
        static void Main(string[] args)
        {
            var lista = Listar(@"E:\Teste");

            foreach (string a in lista)
            {
                Console.WriteLine(a);
            }

            Console.ReadLine();
        }

        static IEnumerable<string> Listar(string caminho)
        {
            var arquivos = Directory.EnumerateFiles(caminho, "*", SearchOption.AllDirectories).Select(Path.GetFileName);

            return arquivos;
        }
    }
}
    
14.10.2015 / 14:00
-4

Use a class like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    public class Dir
    {
        public string DirPath { get; private set; }
        public Dir(string Path)
        {
            if (!System.IO.Directory.Exists(Path)) throw new FormatException("Caminho inválido");
            DirPath = Path;
        }

        public IEnumerable<KeyValuePair<string[], string[]>> GetItems()
        {
            char separator = char.Parse("\");
            foreach(string _dir in System.IO.Directory.GetDirectories(DirPath, "*.*", System.IO.SearchOption.AllDirectories))
            {
                var name = _dir.Split(separator).Last();
                yield return new KeyValuePair<string[], string[]>(new string[2] { _dir, name }, 
                    System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(_dir), "*.*", System.IO.SearchOption.TopDirectoryOnly)
                    .Select(x => System.IO.Path.GetFileName(x))
                    .ToArray());
            }
        }
    }
}

How to use:

Dir _dir = new Dir(@"D:\Temp\php");

IEnumerable<KeyValuePair<string[], string[]>> itens = _dir.GetItems();

foreach(KeyValuePair<string[], string[]> item in itens)
{
    string PathFull = item.Key[0]; // nome completo da pasta
    string namePath = item.Key[1]; // somente o nome da pasta
    System.Console.WriteLine("Pasta: {0}", namePath);
    System.Console.WriteLine("Files");
    foreach (string files in item.Value)
    {
        System.Console.WriteLine("{0}", files); //somente o nome do arquivo
    }
    System.Console.WriteLine("");
    System.Console.WriteLine("-------------------------------------------------------");
    System.Console.WriteLine("");
}

Output example:

    
13.10.2015 / 23:24