Search code examples
c#filedirectory-listing

how do you get File Listing Details on c#


i'm currently doing an assignment at university and i'm struggling on a specific task

After displaying a file listing i need to prompt the user to enter the number of a file to get more details on that file. The user can then enter the number 0 to skip this step. The extra details shown should be:

File: notepad.exe
Full file name: C:\Windows\notepad.exe
File size: 93536 bytes
Created: 14/07/2009 12:54:24
Last accessed: 10/08/2009 15:21:05

im using C# im wondering if anyone knows how to guide me on the right step? thankyou


Solution

  • For general file information, like size and creation and modification times, use the FileInfo class.

    FileInfo f = new FileInfo(@"C:\Windows\Notepad.exe");
    long size = f.Length;
    DateTime creation = f.CreationTime;
    DateTime modification = f.LastWriteTime;
    string name = f.Name; //returns "Notepad.exe"
    //etc...
    

    Alternatively, for getting a filename from a full path, use the Path class.

    string fName = Path.GetFilename(@"C:\Windows\Notepad.exe"); //returns "Notepad.exe"
    

    I'll leave formatting the info string to you.

    Be advised that FileInfo depends on the existence of the file, while the Path methods only deal with string manipulation. The file does not need to exist.