Accessing Files In A Virtual Directory

  • Thread starter Thread starter timburda
  • Start date Start date
T

timburda

Here is the problem:

I need to set HTTP-Headers on specific files in a virtual directory,
but I can't seem to find the files using the DirectoryEntry object.

I am able to successfully set the HTTP-Headers on virtual directories
just fine, but as stated earlier, in some cases I need to set the
HTTP-Headers on specific files only.

Does anyone have any thoughts on how I might be able to achieve this???

Thanks -

Tim Burda
(e-mail address removed)
 
Ooops, by the way, I need to do this programatically NOT using the IIS
admin tool.

Thanks -

Tim
 
OK - I found may answer from another thread (after much searching). So
mad props to the person who actually knew the answer. Unfortunately, I
don't have his name or the link to thread. But hey, this is for the
greater good, so I'm sure he'd want me to post the answer here (even
without creditting him).

The bottom line:

You cannot access the properties of a file under a virtual directory
using the DirectoryEntry object, UNLESS the file has been explicitly
added to the IIS metabase. Just because the file appears under the
virtual directory in the IIS service manger doesn't make this so. In
fact, I don't know for sure how you do this in a non-programmatic
manner.

In any event, the solution:

// CREATE THE VIRUTAL ITEM

DirectoryEntry folderRoot = new DirectoryEntry
("IIS://localhost/W3SVC/1/Root");

folderRoot.RefreshCache();

//Assumes that you have added a virtual directory called "TEST02" --
//how to that is beyond the scope of this answer
DirectoryEntry vDir = folderRoot.Children.Find("TEST02",
folderRoot.SchemaClassName);

//Adds a "virtual file" reference to the metabase called "foo.xml" --
//assumes that the file exists under the virtual directory TEST02
DirectoryEntry vFile = vDir.Children.Add("foo.xml","IIsWebFile");

//Not sure if you need to need to commit all of these changes,
//but it doesn't hurt anything
vFile.CommitChanges();
vDir.CommitChanges();
folderRoot.CommitChanges();


// DELETE THE VIRTUAL DIRECTORY AND ALL CHILDREN

string webItemName = "";

//Find the virtual on the web site
DirectoryEntry root = new
DirectoryEntry("IIS://localhost/W3SVC/1/Root");

//Find the virtual directory "TEST02"
DirectoryEntry vdir = root.Children.Find("TEST02", "IIsWebVirtualDir");


//Removes all of the virtual items (files, etc.) under
//the virtual directory


foreach (DirectoryEntry webitem in vdir.Children)
{
//Remove all the items from the virtual directory
webitemname = webitem.Name;
vdir.Children.Remove(webitem);
vdir.CommitChanges();

Console.WriteLine("Deleted item: " + webItemName + " under virtual
directory: " + virtDirectory);
}

// Remove the virtual directory itself from the web site
root.Children.Remove(vdir);

// Commit all of changes made to the website
root.CommitChanges();
root.Close();
 
Back
Top