how to get file properties?

82,915

Solution 1

When reviewing or opening a file, to get its name, the FileInfo class is equipped with the Name property. Here is an sample code:

FileInfo oFileInfo = new FileInfo(strFilename);

if (oFileInfo != null || oFileInfo.Length == 0)
{
   MessageBox.Show("My File's Name: \"" + oFileInfo.Name + "\"");
   // For calculating the size of files it holds.
   MessageBox.Show("myFile total Size: " + oFileInfo.Length.ToString());
}

You can check like this:

if (!oFileInfo.Exists)
{
    throw new FileNotFoundException("The file was not found.", FileName);
}

To find out what those date and time values are, you can access the File System Information property using:

DateTime dtCreationTime = oFileInfo.CreationTime;
MessageBox.Show("Date and Time File Created: " + dtCreationTime.ToString());

To know the extension of the file, you can access the value of the FileSystemInfo.Extension property:

MessageBox.Show("myFile Extension: " + oFileInfo.Extension);

Solution 2

Here's a link with information about looking at the attributes.

Besides that, the FileInfo class is what you're probably looking to use.

What other kinds of properties are you looking at?

Share:
82,915
ComputerIntelligentAgent
Author by

ComputerIntelligentAgent

Updated on January 29, 2020

Comments

  • ComputerIntelligentAgent
    ComputerIntelligentAgent over 4 years

    I want an application which displays the some file properties of a mediafile if available, like (don't know the exact english words used in windows for it) FileName, Length/Duration, FileType(.avi .mp3 etc.) I tried taglib and windowsapishell but I dont get a working result (references are good)

    ShellFile so = ShellFile.FromFilePath(file);
    so.Properties.System.(everythingIwant)
    

    shows me a lot of file properties which I want to have displayed, but I cant get it working An example of an error:

    'WindowsFormsApplication2.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. The program '[6300] WindowsFormsApplication2.vshost.exe: Program Trace' has exited with code 0 (0x0). The program '[6300] WindowsFormsApplication2.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

    something easy like

    var thing = so.Properties.System.FileName.Description;
    Console.WriteLine(thing);
    

    wont work

    I do know some Java and PHP programming, but I'm totally new to C#


    Special thanks to @marr75 and @errorstacks !

    one follow up question: I made this, and it works

    class Program
    {
        static void Main(string[] args)
        {   
            string file = "E:/Dump/Shutter Island.avi";
    
            FileInfo oFileInfo = new FileInfo(file);
            Console.WriteLine("My File's Name: \"" + oFileInfo.Name + "\"");
            DateTime dtCreationTime = oFileInfo.CreationTime;
            Console.WriteLine("Date and Time File Created: " + dtCreationTime.ToString());
            Console.WriteLine("myFile Extension: " + oFileInfo.Extension);
            Console.WriteLine("myFile total Size: " + oFileInfo.Length.ToString());
            Console.WriteLine("myFile filepath: " + oFileInfo.DirectoryName);
            Console.WriteLine("My File's Full Name: \"" + oFileInfo.FullName + "\"");
    
        }               
    }
    

    but I want it to only provide me with the info if the info exists. I saw the

       **Exists**   Gets a value indicating whether a file exists. (Overrides FileSystemInfo.Exists.)
    

    But how do I use this function, I guess not like if(io.ofileinfo.FullName.exist) {Console.Write(io.ofileinfo.fullname);} ?

    • Vlad
      Vlad over 12 years
      The program seems to run through successfully. The message you presented is not an error message, just usual diagnostics. Did you forget to output the found values? Or maybe you just need to set a breakpoint at the last line of your Main?
    • Jason Williams
      Jason Williams over 12 years
      The .net System.FileInfo class will probably deliver many of the results you need. It won't provide info like duration (of media files) but you may find starting here for the more basic information helps as a stepping stone to a working application.
    • bilash.saha
      bilash.saha over 12 years
    • Imran Faruqi
      Imran Faruqi about 3 years
      I was looking to read details tab in File properties and someone can do this using object returned from -> FileVersionInfo.GetVersionInfo(path);
  • ComputerIntelligentAgent
    ComputerIntelligentAgent over 12 years
    This was exactly what I was looking for!! ;) Many thanks (to all)
  • ComputerIntelligentAgent
    ComputerIntelligentAgent over 12 years
    This was exactly what I was looking for!! ;) Many thanks (to all)