Return the path value

3

How to return the value with the name of a folder in C #?

Example, if I execute:

Directory.GetFiles("%PROGRAMFILES% (x86)\MyApp", "*.*", true);

And if you can do it, it returns like this:

%PROGRAMFILES% (x86)\MyApp\MyApp.exe
%PROGRAMFILES% (x86)\MyApp\MyApp.dll

And what I want is to just return in place of MyApp only \ .

Ex: \MyApp.exe or \MyApp.dll instead of the path to the complete directory.

    
asked by anonymous 27.09.2015 / 17:41

2 answers

4

Use Path.GetFileName() .

var nomeArquivo = Path.GetFileName(path);

Try this:

var arquivos = Directory.EnumerateFiles("%PROGRAMFILES% (x86)\MyApp", "*",
                   SearchOption.AllDirectories).Select(Path.GetFileName);

I have my doubts if this search is what you want, but I did as it was presented.

See almost running on dotNetFiddle . It is not showing anything because there is no file there and I can not access other folders on that machine, but it does not give any error. In the test of my machine listed files.

    
27.09.2015 / 17:46
1
using System.IO;
    // manipular de diretorios
    DirectoryInfo dirInfo = new DirectoryInfo(@"C:\Documents and Settings\etc\etc ");

    // procurar arquivos
    BuscaArquivos(dirInfo);

private void BuscaArquivos(DirectoryInfo dir)
{
     // lista arquivos do diretorio corrente
     foreach (FileInfo file in dir.GetFiles())
    {                
           // aqui no caso estou guardando o nome completo do arquivo em em controle ListBox
           // voce faz como quiser
           lbxResultado.Items.Add(file.FullName);                
    }

    // busca arquivos do proximo sub-diretorio
    foreach (DirectoryInfo subDir in dir.GetDirectories())
    {
          BuscaArquivos(subDir);
    }
}
    
13.10.2015 / 22:55