Get filenames without path of a specific directory

Viewed 110081

How can I get all filenames of a directory (and its subdirectorys) without the full path? Directory.GetFiles(...) returns always the full path!

8 Answers

You can get the files name of particular directory using GetFiles() method of the DirectoryInfo class. Here are sample example to list out all file and it's details of particular directory

System.Text.StringBuilder objSB = new System.Text.StringBuilder();
    System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo("d:\\");
    objSB.Append("<table>");
    objSB.Append("<tr><td>FileName</td>" + 
                 "<td>Last Access</td>" + 
                 "<td>Last Write</td>" + 
                 "<td>Attributes</td>" + 
                 "<td>Length(Byte)</td><td>Extension</td></tr>");

    foreach (System.IO.FileInfo objFile in directory.GetFiles("*.*"))
    {
        objSB.Append("<tr>");

        objSB.Append("<td>");
        objSB.Append(objFile.Name);
        objSB.Append("</td>");

        objSB.Append("<td>");
        objSB.Append(objFile.LastAccessTime);
        objSB.Append("</td>");

        objSB.Append("<td>");
        objSB.Append(objFile.LastWriteTime);
        objSB.Append("</td>");

        objSB.Append("<td>");
        objSB.Append(objFile.Attributes);
        objSB.Append("</td>");

        objSB.Append("<td>");
        objSB.Append(objFile.Length);
        objSB.Append("</td>");

        objSB.Append("<td>");
        objSB.Append(objFile.Extension);
        objSB.Append("</td>");

        objSB.Append("</tr>");
    }
    objSB.Append("</table>");

    Response.Write(objSB.ToString());

This example display list of file in HTML table structure.

Related