How to list all files from a server directory in ASP.NET MVC?

1

I want to know how to create a List<string> with the names of files in a given directory.

The service is hosted on a shared server, published in my httpdocs .

I have several images within httpdocs/img/imagens .

How do I access this directory?

Controller code:

DirectoryInfo diretorio = new DirectoryInfo("~/img/imagens/");
FileInfo[] arquivos = diretorio.GetFiles();

Error:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
    Could not find a part of the path 'C:\Windows\SysWOW64\inetsrv\~\img\imagens'.
</string>
    
asked by anonymous 15.05.2015 / 04:32

1 answer

2

That's almost it. Change to:

DirectoryInfo diretorio = new DirectoryInfo(Server.MapPath("~/img/imagens/"));
FileInfo[] arquivos = diretorio.GetFiles();

See more here .

    
15.05.2015 / 07:50