posts - 0, comments - 1573, trackbacks - 0

Creating a virtual directory using System.EnterpriseServices and C#

I came across this post in the IIS Newsgroups today


I want to create virtual directory in IIS. I have a method in doing C# for doing this . The following code is supposed do my job. But It throws the following errors. I strongly feel there is something going wrong with the metabase path "IIS://localhost/W3SVC/1/Root".

Can somebody help me in finding out correct metabase path.

//Create a virtual directory

System.EnterpriseServices.Internal.IISVirtualRoot vr = new System.EnterpriseServices.Internal.IISVirtualRoot();

string sError;

vr.Create("IIS://localhost/W3SVC/1/Root",@"C:\Demos\WebServices\","WebServices",out sError);

The error it has thrown is :

System.Runtime.InteropServices.COMException (0x80070003): The system cannot find the path specified
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_IsContainer()
at System.DirectoryServices.DirectoryEntries.CheckIsContainer()
at System.DirectoryServices.DirectoryEntries.Add(String name, String schemaClassName)
at System.EnterpriseServices.Internal.IISVirtualRoot.Create(String RootWeb, String inPhysicalDirectory, String VirtualDirectory, String Error)
 

In my search to try to help this person I came across the original location of the code he used at
http://blogs.officezealot.com/charles/archive/2004/10/26/3191.aspx

I never know that there was a System.EnterpriseServices.Internal.IISVirtualRoot object in .NET so I learned something along the way as well.

Note: If testing this you will need to add a reference to System.EnterpriseServices

I did some simple tests to try to reproduce the error that the user has seen. I managed to get the same error by specifying an invalid metabase path which explains the The system cannot find the path specified message.

In the code above the IIS Metabase path is specified as /W3SVC/1/Root. The digit 1 (one) in the code refers to the Web Site with an Instance ID of 1. This is always the default web site.

The way to determine what your web site instance id is; is to look at the log file name.

The Log filename contains a path such as W3SVCx where x is the web site Instance ID.

Open IIS Manager and select the web site.

Right click it and select properties and the following will be displayed.

Click on the properties button to bring up the Extended logging properties dialog.

In the highlighted area in the image above you will see the web site Instance ID.

Replace the number in the code with this number and it should work fine for you.

Print | posted on Tuesday, May 30, 2006 12:20 AM |

Feedback

Gravatar

# re: Creating a virtual directory using System.EnterpriseServices and C#

Thanks!
9/22/2007 7:40 AM | Craig Boline
Gravatar

# re: Creating a virtual directory using System.EnterpriseServices and C#

Hi All,

I want to programmatically(c# fmw 2.0) create a folder under my IIS Web Site. I figured that I needed to create an entry in the IIS Metabase.

So, after investigating a while and i figured out something pretty weird:

I downloaded a source code that allowed me to do what I wanted. However, it's written in asp.net v1.0 and I needed in v2.0. So, I converted it to that version.

Well, you all can imagine it must have worked, but you're wrong: it didn't (i'm pretty sure is that because i tried: copying the procedures and functions I needed to a asp.net v2.0 project. Then, after the last try didn't work, I tried converting the entire solution to v2.0 and it did it successfully).

I don't know why, but the v1.0 application is able to create the entry at the metabase under my virtual directory(IIS://localhost/W3SVC/<web site id>/root/<new dir's name>) but when I try to do the same using the v2.0 app i get the following error:

System.Runtime.InteropServices.COMException (0x80070003): The system cannot find the path specified.

at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.PropertyValueCollection.PopulateList()
at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
at Example.Form1.setDirectory_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Do you have any clue on this?

Thanks,

Brian
9/17/2008 7:32 AM | Brian Roisentul
Gravatar

# re: Creating a virtual directory using System.EnterpriseServices and C#

same issue here...the code seems to work fine with 1.1 but doesnt work with 2.0!!any clues!
10/7/2008 1:49 PM | JR

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 2 and 5 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET