Creating virtual directory in IIS using C#

S

sudhapadmas

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(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&.....


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
 
G

Guest

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
 

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

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top