how to get file properties?

ComputerIntelligentAgent picture ComputerIntelligentAgent · Oct 22, 2011 · Viewed 76.3k times · Source

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);} ?

Answer

Enigma State picture Enigma State · Oct 22, 2011

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);