Recuperare il content type
Stufo dei soliti switch per recuperare il content type di un file, mi sono deciso a dare una sbirciata in rete per verificare se esistesse un qualcosa che mi potesse restituire il content type dato un file name.
Ovviamente, come quasi sempre in questi casi, qualcun’altro si è posto la mia stessa domanda ed ha trovato una soluzione a tale problema .
Di fatto, nel .NET Framework è possibile utilizzare la classe Registry per andare a recuperare la nostra informazione, come mostrato dall’extension method seguente:
/// <summary>
/// Retrieve the mimetype for the specified filename.
/// </summary>
/// <param name = "fileName">Name of the file.</param>
/// <returns></returns>
public static string GetMimeType (this string fileName ) {
string mime = "application/octetstream";
string ext = Path.GetExtension ( fileName ).ToLower ( );
RegistryKey rk = Registry.ClassesRoot.OpenSubKey ( ext );
if ( rk != null && rk.GetValue ( "Content Type" ) != null ) {
mime = rk.GetValue ( "Content Type" ).ToString ( );
}
return mime;
}
Ciauz
FONTE : http://petrocel.wordpress.com/2008/01/09/how-to-get-the-content-typemimetype-of-a-file-c/