Sample working code I use:
using System;
using System.Director yServices;
namespace PAB.Utils
{
public class MkVdir
{
public MkVdir()
{
}
public static string CreateVDir(stri ng WebSite, string VDirName, string
Path, bool RootDir, bool chkRead,bool chkWrite, bool chkExecute, bool
chkScript, bool chkAuth,int webSiteNum, string serverName)
{
string sRet=String.Emp ty;
System.Director yServices.Direc toryEntry IISSchema;
System.Director yServices.Direc toryEntry IISAdmin;
System.Director yServices.Direc toryEntry VDir;
bool IISUnderNT;
IISSchema = new System.Director yServices.Direc toryEntry("IIS://"
+serverName +"/Schema/AppIsolated");
if (IISSchema.Prop erties["Syntax"].Value.ToString ().ToUpper() ==
"BOOLEAN")
IISUnderNT = true;
else
IISUnderNT = false;
IISAdmin = new
System.Director yServices.Direc toryEntry("IIS://localhost/W3SVC/" + webSiteNum
+ "/Root");
if (!RootDir)
{
foreach(System. DirectoryServic es.DirectoryEnt ry v in IISAdmin.Childr en)
{
if (v.Name == VDirName)
{
// Delete the specified virtual directory if it already exists
try
{
IISAdmin.Invoke ("Delete", new string [] { v.SchemaClassNa me, VDirName
});
IISAdmin.Commit Changes();
}
catch(Exception ex)
{
sRet+=ex.Messag e;
}
}
}
}
//
// Create the virtual directory
//
if (!RootDir)
{
VDir = IISAdmin.Childr en.Add(VDirName , "IIsWebVirtualD ir");
}
else
{
VDir = IISAdmin;
}
//
// Setup the VDir
//
VDir.Properties["AccessRead "][0] = chkRead;
VDir.Properties["AccessExec ute"][0] = chkExecute;
VDir.Properties["AccessWrit e"][0] = chkWrite;
VDir.Properties["AccessScri pt"][0] = chkScript;
VDir.Properties["AuthNTLM"][0] = chkAuth;
VDir.Properties["EnableDefaultD oc"][0] = true;
VDir.Properties["EnableDirBrows ing"][0] = false;
VDir.Properties["DefaultDoc "][0] = true;
VDir.Properties["Path"][0] = Path;
VDir.Properties["AppFriendlyNam e"][0]=VDirName;
//
// NT doesn't support this property
//
if (!IISUnderNT)
{
VDir.Properties["AspEnableParen tPaths"][0] = true;
}
VDir.CommitChan ges();
if (IISUnderNT)
{
VDir.Invoke("Ap pCreate", false);
}
else
{
VDir.Invoke("Ap pCreate", 1);
}
sRet+= "VRoot " +VDirName + " created!";
return sRet;
}
}
}
--Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
"su*********@ya hoo.com" wrote:
Hello netters,
I was trying to create a virtual directory in IIS using the following
code:
System.Enterpri seServices.Inte rnal.IISVirtual Root vr = new
System.Enterpri seServices.Inte rnal.IISVirtual Root();
string sError;
vr.Create("IIS://localhost/W3SVC/1/Root",@"C:\Prog ram
Files\Soft","We bServices",out sError);
Console.WriteLi ne(sError);
But I am getting following errors:
System.Runtime. InteropServices .COMException (0x80070003): The system
cannot find the path specified
at System.Director yServices.Direc toryEntry.Bind( Boolean throwIfFail)
at System.Director yServices.Direc toryEntry.Bind( )
at System.Director yServices.Direc toryEntry.get_I sContainer()
at System.Director yServices.Direc toryEntries.Che ckIsContainer()
at System.Director yServices.Direc toryEntries.Add (String name, String
schemaClassName )
at System.Enterpri seServices.Inte rnal.IISVirtual Root.Create(Str ing
RootWeb, String inPhysicalDirec tory, String VirtualDirector y,
String&.....
I even tried to create virtual directory using System.Director yEntry.
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