I'm trying to set a MIME type for a certain file extension (.jdf) using MimeMapClass in C#.
Following is the code I use which works fine.
// directory is a virtual directory, say BSS
// extension = ".jdf"
// type = "text/xml"
private static void SetMIMEType(DirectoryEntry directory, string extension, string type)
{
bool MIMETypeExists = false;
//Retrieve the MimeMap of [directory]
PropertyValueCollection propertyValues = directory.Properties["MimeMap"];
if (propertyValues.Count > 0)
{
//Check if the MIME type already exists in the directory
foreach (object obj in propertyValues)
{
if (((IISMimeType)obj).Extension == extension)
{
MIMETypeExists = true;
break;
}
}
}
//If the MIME type does not exist, it is added to the MIME map of [directory]
if (MIMETypeExists == false)
{
MimeMapClass tempMIMEObj = new MimeMapClass();
tempMIMEObj.Extension = extension;
tempMIMEObj.MimeType = type;
directory.Properties["MimeMap"].Add(tempMIMEObj);
}
}
What it does is, retieve the MimeMap of [directory] which is a Virtual Directory listed under [Default Web Site] in my IIS Manager. Now it works fine under usual circumstances. However, a problem arises when my [directory], let's call it BSS, inherits the file extention (.jdf) to which I try to set MIME type from above.
Let me elaborate more. Here is my file structure in IIS Manager.
[ComputerName\User]
|
|
Site
|
|
Default Web Site
|
|
BSS
Now I manually add a MIME type to the file extension [.jdf] in, say, [Default Web Site]. When I do that my IIS Manager automatically inherits this extension and it's value to BSS from Defult Web Site. However, if I run my code sample now it does not know this and still tries to register a MIME type for the same extension which causes an error.
How can I know from within C# that if the folder to which I'm trying to set a MIME type has already inherited the said file extension from a folder above (be it Default Web Site or anything above either)?
NOTE :
I'm using Windows Vista but my program should be able to run on a variety of OS from Windows Server 2003 to Windows 7.