472,127 Members | 1,627 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Creating virtual directory in IIS using C#

Hello netters,

I was trying to create a virtual directory in IIS using the following
code:
System.EnterpriseServices.Internal.IISVirtualRoot vr = new
System.EnterpriseServices.Internal.IISVirtualRoot( );
string sError;
vr.Create("IIS://localhost/W3SVC/1/Root",@"C:\Program
Files\Soft","WebServices",out sError);
Console.WriteLine(sError);
But I am getting following errors:
System.Runtime.InteropServices.COMException (0x80070003): The system
cannot find the path specified
at System.DirectoryServices.DirectoryEntry.Bind(Boole an throwIfFail)

at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_IsCont ainer()
at System.DirectoryServices.DirectoryEntries.CheckIsC ontainer()
at System.DirectoryServices.DirectoryEntries.Add(Stri ng name, String

schemaClassName)
at System.EnterpriseServices.Internal.IISVirtualRoot. Create(String
RootWeb, String inPhysicalDirectory, String VirtualDirectory,
String&.....
I even tried to create virtual directory using System.DirectoryEntry.
It gives me the same error.
Iam sure C:\Program Files\Soft exists . I am doubtful if Iam making
some mistake in passing first parameter.
Can Somebody please tell me what is going wrong?
Thanking you in advance,
Sudha

May 26 '06 #1
1 4790
Sample working code I use:
using System;
using System.DirectoryServices;
namespace PAB.Utils
{
public class MkVdir
{
public MkVdir()
{
}
public static string CreateVDir(string WebSite, string VDirName, string
Path, bool RootDir, bool chkRead,bool chkWrite, bool chkExecute, bool
chkScript, bool chkAuth,int webSiteNum, string serverName)
{
string sRet=String.Empty;
System.DirectoryServices.DirectoryEntry IISSchema;
System.DirectoryServices.DirectoryEntry IISAdmin;
System.DirectoryServices.DirectoryEntry VDir;
bool IISUnderNT;
IISSchema = new System.DirectoryServices.DirectoryEntry("IIS://"
+serverName +"/Schema/AppIsolated");
if (IISSchema.Properties["Syntax"].Value.ToString().ToUpper() ==
"BOOLEAN")
IISUnderNT = true;
else
IISUnderNT = false;
IISAdmin = new
System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/" + webSiteNum
+ "/Root");
if (!RootDir)
{
foreach(System.DirectoryServices.DirectoryEntry v in IISAdmin.Children)
{
if (v.Name == VDirName)
{
// Delete the specified virtual directory if it already exists
try
{
IISAdmin.Invoke("Delete", new string [] { v.SchemaClassName, VDirName
});
IISAdmin.CommitChanges();
}
catch(Exception ex)
{
sRet+=ex.Message;
}
}
}
}

//
// Create the virtual directory
//
if (!RootDir)
{
VDir = IISAdmin.Children.Add(VDirName, "IIsWebVirtualDir");
}
else
{
VDir = IISAdmin;
}

//
// Setup the VDir
//
VDir.Properties["AccessRead"][0] = chkRead;
VDir.Properties["AccessExecute"][0] = chkExecute;
VDir.Properties["AccessWrite"][0] = chkWrite;
VDir.Properties["AccessScript"][0] = chkScript;
VDir.Properties["AuthNTLM"][0] = chkAuth;
VDir.Properties["EnableDefaultDoc"][0] = true;
VDir.Properties["EnableDirBrowsing"][0] = false;
VDir.Properties["DefaultDoc"][0] = true;
VDir.Properties["Path"][0] = Path;
VDir.Properties["AppFriendlyName"][0]=VDirName;
//
// NT doesn't support this property
//
if (!IISUnderNT)
{
VDir.Properties["AspEnableParentPaths"][0] = true;
}
VDir.CommitChanges();
if (IISUnderNT)
{
VDir.Invoke("AppCreate", false);
}
else
{
VDir.Invoke("AppCreate", 1);
}
sRet+= "VRoot " +VDirName + " created!";
return sRet;
}
}
}

--Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"su*********@yahoo.com" wrote:
Hello netters,

I was trying to create a virtual directory in IIS using the following
code:
System.EnterpriseServices.Internal.IISVirtualRoot vr = new
System.EnterpriseServices.Internal.IISVirtualRoot( );
string sError;
vr.Create("IIS://localhost/W3SVC/1/Root",@"C:\Program
Files\Soft","WebServices",out sError);
Console.WriteLine(sError);
But I am getting following errors:
System.Runtime.InteropServices.COMException (0x80070003): The system
cannot find the path specified
at System.DirectoryServices.DirectoryEntry.Bind(Boole an throwIfFail)

at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_IsCont ainer()
at System.DirectoryServices.DirectoryEntries.CheckIsC ontainer()
at System.DirectoryServices.DirectoryEntries.Add(Stri ng name, String

schemaClassName)
at System.EnterpriseServices.Internal.IISVirtualRoot. Create(String
RootWeb, String inPhysicalDirectory, String VirtualDirectory,
String&.....
I even tried to create virtual directory using System.DirectoryEntry.
It gives me the same error.
Iam sure C:\Program Files\Soft exists . I am doubtful if Iam making
some mistake in passing first parameter.
Can Somebody please tell me what is going wrong?
Thanking you in advance,
Sudha

May 26 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Leszek | last post: by
3 posts views Thread by Stelrad Doulton | last post: by
7 posts views Thread by Jaydeep | last post: by
15 posts views Thread by David Thielen | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.