Get filenames without path of a specific directory

前端 未结 8 1069
半阙折子戏
半阙折子戏 2020-12-13 16:44

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

8条回答
  •  攒了一身酷
    2020-12-13 17:27

    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("");
        objSB.Append("" + 
                     "" + 
                     "" + 
                     "" + 
                     "");
    
        foreach (System.IO.FileInfo objFile in directory.GetFiles("*.*"))
        {
            objSB.Append("");
    
            objSB.Append("");
    
            objSB.Append("");
    
            objSB.Append("");
    
            objSB.Append("");
    
            objSB.Append("");
    
            objSB.Append("");
    
            objSB.Append("");
        }
        objSB.Append("
    FileNameLast AccessLast WriteAttributesLength(Byte)Extension
    "); objSB.Append(objFile.Name); objSB.Append(""); objSB.Append(objFile.LastAccessTime); objSB.Append(""); objSB.Append(objFile.LastWriteTime); objSB.Append(""); objSB.Append(objFile.Attributes); objSB.Append(""); objSB.Append(objFile.Length); objSB.Append(""); objSB.Append(objFile.Extension); objSB.Append("
    "); Response.Write(objSB.ToString());

    This example display list of file in HTML table structure.

提交回复
热议问题