Click here to Skip to main content
16,014,860 members
Articles / Programming Languages / Visual Basic
Article

How to create a virtual directory with C#

Rate me:
Please Sign up or sign in to vote.
4.30/5 (24 votes)
5 Jul 2001 297.5K   6.4K   59   45
A simple example of using the DirectoryServices namespace in .NET

Sample Image - virtualdir.jpg

Introduction

This "how to" is only one more sample of how to use the DirectoryServices namespace in the Microsoft.Net framework. This sample uses IIS as its Active Directory provider. The access to Active Directory in .Net is so easy. You use System.DirectoryServices.dll for simple access.

I implemented a simple class called IISManager, it has two methods: Connect and CreateVirtualDirectory.

// To use it...
            
// Default constructor initialize the servername to localhost
// you could use IISManager(servername) 
IISManager isMang = new IISManager();
isMang.Connect();
try
{
	isMang.CreateVirtualDirectory(txtName.Text,txtPath.Text);
}
catch (Exception ex)
{
	MessageBox.Show(ex.Message);
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Uruguay Uruguay
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: How do you delete Virtual Directory? Pin
Anonymous18-Nov-04 22:55
Anonymous18-Nov-04 22:55 
AnswerRe: How do you delete Virtual Directory? Pin
kalph12-Dec-05 22:13
kalph12-Dec-05 22:13 
AnswerRe: How do you delete Virtual Directory? Pin
kalph12-Dec-05 22:17
kalph12-Dec-05 22:17 
AnswerRe: How do you delete Virtual Directory? Pin
Member 314043814-Jan-08 0:58
Member 314043814-Jan-08 0:58 
AnswerRe: How do you delete Virtual Directory? Pin
Member 314043814-Jan-08 1:00
Member 314043814-Jan-08 1:00 
GeneralI solved this problem. Pin
kind51114-May-02 22:16
kind51114-May-02 22:16 
GeneralRe: I solved this problem. Pin
Orion Buttigieg17-May-02 6:29
Orion Buttigieg17-May-02 6:29 
GeneralRe: I solved this problem. Pin
kind5119-Jun-02 16:46
kind5119-Jun-02 16:46 
Although, i didn't test on WinXP, reason may be path problem. If path is not real path, IIS show there icon on red round box. That means you can't access that url.

There are problem in my previos createvdir method. It create vdir on default iis website. It looks fine, but don't execute asp.net webapp. Because, their vdir doesn't have AppName. To create AppName(Really AppFriendlyName), you must run directory's AppCreate method.

Below source is latest code. In my thinking, code works fine. vDir.Invoke method is to run adsi'method. It's very hard to this methodlogy.

You wanna find other iiswebdirectory's property and method, search VS.NET's Online Documentation. When you don't know where it exists, you find 'AppCreate' in search category, and sync location.

Have a nice day, friends...^^;
And sorry my poor english.

public static void CreateVDir(string sPhysicalPath, string sVirDirName)
{
System.DirectoryServices.DirectoryEntry oDE;
System.DirectoryServices.DirectoryEntries oDC;
System.DirectoryServices.DirectoryEntry oVirDir;
string sIISPath;

try
{
sIISPath = "IIS://" + _sHost + "/W3SVC/" + _sServer + "/Root";
oDE = new DirectoryEntry(sIISPath);
oDC = oDE.Children;
oVirDir = oDC.Add(sVirDirName,oDE.SchemaClassName.ToString());
oVirDir.CommitChanges();

oVirDir.Properties["Path"].Value = sPhysicalPath;
oVirDir.Properties["AccessRead"][0] = true;
oVirDir.Invoke("AppCreate",true);
oVirDir.Properties["AppFriendlyName"][0] = sVirDirName;

oVirDir.CommitChanges();
}
catch (Exception exc)
{
throw new Exception(exc.Message);
}
}
GeneralRe: I solved this problem. Pin
Orion Buttigieg10-Jun-02 4:47
Orion Buttigieg10-Jun-02 4:47 
GeneralRe: I solved this problem. Pin
kwik220-Mar-06 0:37
kwik220-Mar-06 0:37 
GeneralPath not set Pin
Kannan Kalyanaraman17-Mar-02 2:32
Kannan Kalyanaraman17-Mar-02 2:32 
GeneralI Have the same problem Pin
SMORGS26-Feb-03 12:17
SMORGS26-Feb-03 12:17 
GeneralRe: I Have the same problem Pin
Anonymous18-Mar-04 17:03
Anonymous18-Mar-04 17:03 
GeneralRe: I Have the same problem Pin
hackaroy28-Apr-04 13:11
hackaroy28-Apr-04 13:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.