File content type / MIME type of a file C#



This method take the file content type of a file As an example if a image file then it is a "image/jpeg" or if it is a notepad then it will be "text/plain".

This will be helpful mostly when storing files as byte[] in the database and specially when retrieving those files. It help full to system to identify the files type. This content something looks like the extension type of the format. But it is not exactly extension.Content type help to categories the files to system.


 private string GetMimeType(FileInfo fileInfo)
        {
            string mimeType = "application/unknown";

            RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(
                fileInfo.Extension.ToLower()
                );

            if (regKey != null)
            {
                object contentType = regKey.GetValue("Content Type");

                if (contentType != null)
                    mimeType = contentType.ToString();
            }

            return mimeType;
        }

Comments

Popular Posts