user3271703
user3271703

Reputation: 37

Trouble with file properties

I want to list all the files and all its properties (name,size,etc) in a directory but i only managed to get the names of the files. here's the code

 <table>
    @foreach(var fileName in Directory.GetFiles("C:/baba"))
    {
    <tr><td>@Path.GetFileName(fileName)</td></tr>
    }
 </table>

Upvotes: 1

Views: 63

Answers (2)

Kevin
Kevin

Reputation: 4848

Perhaps something like this:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)

        {
            var dir = new DirectoryInfo(@"C:\Windows");

            foreach (var file in dir.GetFiles())
            {
                Console.WriteLine("Name: " + file.Name + "\r\nSize: " + file.Length + "\r\nType: " + file.GetType());
            }
            Console.ReadKey();

        }
    }
}

Gives the following output: Program output

Upvotes: 0

dee-see
dee-see

Reputation: 24078

Use the DirectoryInfo class instead. It returns an object instead of a string and it contains more information. The equivalent method would be DirectoryInfo.GetFiles.

Upvotes: 2

Related Questions