C# code : get content type metadata for a given filepath

///
/// Reads content type metadata for a given filepath
///

///
/// Content type of the file
private string ReadContentTypeMetadata(string filepath)
{
const string DefaultContentType = "application/unknown";

RegistryKey regkey = null;
RegistryKey fileextkey = null;
string filecontenttype = null;
string fileextension = null;

//the file extension to lookup
fileextension = Path.GetExtension(filepath);

try
{
//look in HKClassRoot in registry
regkey = Registry.ClassesRoot;
//look for extension
fileextkey = regkey.OpenSubKey(fileextension);
//retrieve Content Type value
filecontenttype = fileextkey.GetValue("Content Type", DefaultContentType).ToString();
//cleanup
fileextkey = null;
regkey = null;
}
catch
{
filecontenttype = DefaultContentType;
}
//string convertedMediatype = convertMediaType(filecontenttype);
return filecontenttype;
}

No comments:

Post a Comment