How to set HttpExpires property on a folder within a virtual directory?

M

ManagedCoder

Hi,

My requirement is as follows:
I need to set the HttpExpires (enable content expiration - set to 7 days) on
a folder within a virtual directory.

I have been able to set the HttpExpires property on a virtual directory
using the following code:

DirectoryEntry myVdPath = new
DirectoryEntry("IIS://localhost/W3SVC/1/Root/<virtual directory>");
myVdPath.RefreshCache();
myVdPath.Properties["HttpExpires"].Value = "D, 604800"; //Content expiration
after 7 days
myVdPath.CommitChanges();

But, if I modify the path and use "IIS://localhost/W3SVC/1/Root/<virtual
directory>/<directory on which the property is to be set>", then it throws
the following exception:

An unhandled exception of type 'System.Runtime.InteropServices.COMException'
occurred in system.directoryservices.dll

Additional information: The system cannot find the path specified

Regards,

Shailesh Patel
 
W

Walter Wang [MSFT]

Hi Shailesh,

Welcome to MSDN Managed Newsgroup!

Here the DirectoryEntry's constructor can only open an entry with schema
class "IIsWebVirtualDir" (configured as a virtual directory in IIS), not
"IIsWebDirectory" (a normal subfolder in a virtual directory). We can get
the reference to the subfolder from its parent entry's Children collection:

DirectoryEntry myVdPath = new
DirectoryEntry("IIS://localhost/W3SVC/1/Root/<virtual directory>");
DirectoryEntry mySubFolder = myVdPath.Children.Find("bin",
"IIsWebDirectory");


Hope this helps. Please feel free to let me know if there's anything
unclear. Thanks.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

ManagedCoder

Hello Walter,

Thanks for your quick response.

I am getting the following exception when I try to access the folder within
a virtual directory.

"An unhandled exception of type 'System.IO.DirectoryNotFoundException'
occurred in system.directoryservices.dll
Additional information: The system cannot find the path specified."

The following code was used:

DirectoryEntry myVdPath = new
DirectoryEntry("IIS://localhost/W3SVC/1/Root/<virtual directory name>");
myVdPath.RefreshCache();
DirectoryEntry mySubDirPath = myVdPath.Children.Find("bin",
"IIsWebDirectory");
mySubDirPath.RefreshCache();

The exception is thrown at line #3 (DirectoryEntry mySubDirPath =
myVdPath...).
I have also tried using "IIsWebVirtualDir" instead of "IIsWebDirectory" and
even defined just the folder name in the Find method,
but no success yet on this issue.

Regards,

Shailesh Patel
 
W

Walter Wang [MSFT]

Hi Shailesh,

Could you please check if the "bin" subfolder exists in the virtual
directory? For example: if we're open
"IIS://localhost/W3SVC/1/Root/WebSite1" and it's mapped to physical folder
"c:\inetpub\wwwroot\WebSite1" on the IIS server, make sure the folder
"c:\inetpub\wwwroot\Website1\bin" exists.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Shailesh,

Have you seen my above reply? Please feel free to let me know if there's
anything unclear. Thanks.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

ManagedCoder

Hi Walter,

As per your suggestion, I tried to access the "bin" folder contained within
a virtual directory.

First, I tried to access the bin folder for a sample project "Sample" which
is stored at physical location "C:\Inetpub\wwwroot\Sample" and
has the following path in IIS - "IIS://localhost/W3SVC/1/ROOT/Sample". In
this case, it worked fine and I was able to set the "HttpExpires" property
on the "bin" folder.

But, I want to access the folder and set "HttpExpires" property on a folder
which is stored in a virtual directory within a parent virtual directory.

Hence, in IIS the virtual directories are displayed as:

Default Web Site
-- MainProject (virtual directory) (physical path -
C:\Inetpub\wwwroot\SampleProject\Site\MainProject)
---- SubProjects (folder)
------ Project1 (another virtual directory) (physical path -
C:\Inetpub\wwwroot\SampleProject\Site\MainProject\SubProjects\Project1)
-------- images (folder on which the HttpExpires property is to be set)

The "MainProject" virtual directory was created using IIS Manager and
"Project1" virtual directory is created using a web application.

I can access both the virtual directory using the following code:

DirectoryEntry myVdPath = new
DirectoryEntry("IIS://localhost/W3SVC/1/ROOT/MainProject");
myVdPath.RefreshCache();

and

DirectoryEntry myVdPath = new
DirectoryEntry("IIS://localhost/W3SVC/1/ROOT/MainProject/SubProjects/Project1");
myVdPath.RefreshCache();

But, when I try to access "images" folder within it, the following error is
thrown:

An unhandled exception of type 'System.Runtime.InteropServices.COMException'
occurred in system.directoryservices.dll
Additional information: Exception from HRESULT: 0x80005008.

Regards,

Shailesh Patel
 
W

Walter Wang [MSFT]

Hi Shailesh,

Sorry, I forgot to mention that the metabase record for a web directory is
not always present in the IIS metabase database. My previous example is
accidentally using the 'bin' directory which happen to have already created
that record.

We need to first to check if it exists or not, if not, then we need to
create it:


static void test2()
{
DirectoryEntry vd1 = new
DirectoryEntry("IIS://wawangvsrv1/W3SVC/1/Root/RoundedCorner/bin/_vti_cnf");
vd1.RefreshCache();

DirectoryEntry deChild = null;
foreach (DirectoryEntry de in vd1.Children)
{
if (de.Name.ToLower() == "abc" && de.SchemaClassName ==
"IIsWebDirectory")
{
deChild = de;
break;
}
}

if (deChild == null)
{
deChild = vd1.Children.Add("abc", "IIsWebDirectory");
vd1.CommitChanges();
}

deChild.Properties["HttpExpires"].Value = "D, 604800";
//Content expiration after 7 days
deChild.CommitChanges();
}


To inspect the IIS metabase database, you can try IIS Metabase Explorer:

#The IIS 6.0 Resource Kit Tools
http://support.microsoft.com/kb/840671


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

ManagedCoder

Hi Walter,

Thank you Walter, for your help in solving this issue and the sample code.

Regards,

Shailesh Patel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top