how to display the files of a folder on a website

1

Currently I have a very simple console app that shows me the names of the files that are inside a specific folder.

public void archivoCarpeta()
{
    string carpeta = ConfigurationManager.AppSettings["carpetaDescarga"];
    DirectoryInfo dir = new DirectoryInfo(carpeta);

    foreach (FileInfo file in dir.GetFiles())
    {
        Console.WriteLine(file.Name);
    }

    Console.Read();
}

but what I need now is to capture the name and date of the files and pass it to a cshtml view and put it inside a table If someone has an idea or a link with an example to guide me, I would appreciate it.

    
asked by Hector 29.08.2018 в 03:22
source

1 answer

1

A simple way is to dynamically create the HTML table in your codebehind

Frontal

You define a table on your front with runat="server" and% id :

<table id="tblFich" runat="server"></table>

Codebehind (server)

In your method archivoCarpeta() you dynamically create the table (with its rows and columns) and assign the value of the file name ( file.Name ) and creation date ( file.CreationTime.ToString() )

public void archivoCarpeta()
        {
            string carpeta = ConfigurationManager.AppSettings["carpetaDescarga"];
            DirectoryInfo dir = new DirectoryInfo(carpeta);

            foreach (FileInfo file in dir.GetFiles())
            {
                HtmlTableRow tr = new HtmlTableRow();
                HtmlTableCell td = new HtmlTableCell();
                td.InnerHtml = file.Name;
                tr.Cells.Add(td);
                td = new HtmlTableCell();
                td.InnerHtml = file.CreationTime.ToString();
                tr.Cells.Add(td);
                tblFich.Rows.Add(tr);
            }
        }

Note : I'm assuming you're not going to have a console application anymore, you're going to move that code to a web application

    
answered by 29.08.2018 / 10:31
source