Donald N. Mafa
Donald N. Mafa

Reputation: 5283

Reading file properties from windows file system?

Would anyone be able to help me with how to get the file properties from a windows object, i.e., FileSize, FileType, Year, Label, DateModified,FileVersion. I have tried accessing the info in the FileInfo class and it appears it does not have all the necessary properties I'm looking for. Which other libraries could I use to access this information and if you could please provide examples, thank you

Upvotes: 0

Views: 1012

Answers (2)

Mike Zboray
Mike Zboray

Reputation: 40818

Some of that is already avaiable in FileInfo (Length is the file size, Date Modified is simply LastWriteTime). Some of the information is available from FileVersionInfo. The 'type' is kind of tricky, but this post has some info on looking the mime type up in the registry. This worked for me on windows 7:

private static string GetType(string fileName)
    {
        string type = "Unknown";
        string ext = System.IO.Path.GetExtension(fileName).ToLower();
        Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
        if (regKey != null && regKey.GetValue("") != null)
        {
            string lookup = regKey.GetValue("").ToString();
            if (!string.IsNullOrEmpty(lookup))
            {
                var lookupKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(lookup);
                if (lookupKey != null)
                {
                    type = lookupKey.GetValue("").ToString();
                }
            }
        }
        return type;
    }

It will produce the Type you see in detail tab page of the file properties. For example, 'Application' for exe and 'Bitmap Image' for bmp.

The answer here gets the type using the windows api function shgetfileinfo.

Upvotes: 1

Sanjay Goswami
Sanjay Goswami

Reputation: 1386

Hey Check with MSDN : http://msdn.microsoft.com/en-us/library/system.io.file.aspx

Example

GetCreationTime ()
GetLastWriteTime ()

Upvotes: 0

Related Questions