Return a similar path value

Asked

Viewed 181 times

3

No result in my post, i would like to know how the function would be used Directory.GetFiles, 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 list = List(@"C: Windows Inf"); foreach(string a in list){ Console.Writeline(a); } Console.Readline(); Console.Exit(); }

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

Ex: There is a folder named after file clerk he would return instead of C: Windows Inf arquivos0\ only \files\

OBS: I was told the following: Execute:

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 the reference: Directory.EnumerateFiles does not exist as I only use version 4.0 of Microsoft . NET Framework

  • Sorry but not understood the question, do you want to recover only the file name? name of the files of a particular folder?

  • 1

    The method Directory.Enumeratefiles exists in . Net 4.0. You must reference the mscorlib Assembly and declare using System.IO;

  • Ah yes! so I can’t see the Directory.EnumerateFiles. I thought this function applied to . NET 3.5 Thank you @ramaral

2 answers

4


First of all, you couldn’t use Directory.EnumerateFiles() because you must not have imported the namespace System.IO, because it works on . 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 take only the main directory, change the third parameter of Directory.EnumerateFiles() of SearchOption.AllDirectories for 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;
        }
    }
}

-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:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.