Click here to Skip to main content
15,896,201 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 294.7K   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: I am unable to set a path in the real path Pin
WIROL18-Oct-07 0:09
WIROL18-Oct-07 0:09 
GeneralVirtual Directory not showing in Internet Services Manager Pin
SeattleCoder23-Jun-03 11:04
SeattleCoder23-Jun-03 11:04 
GeneralRe: Virtual Directory not showing in Internet Services Manager Pin
Anonymous28-Jun-05 0:45
Anonymous28-Jun-05 0:45 
QuestionHow to Create a VD in non-Default web Site Pin
jshi21-Jun-03 8:51
jshi21-Jun-03 8:51 
AnswerRe: How to Create a VD in non-Default web Site Pin
PhoneTech10-Jul-03 6:22
PhoneTech10-Jul-03 6:22 
GeneralApplicationProtection Property. Pin
Kent Moyle28-May-03 17:49
Kent Moyle28-May-03 17:49 
GeneralRe: ApplicationProtection Property. Pin
Member 145133319-Oct-04 5:02
Member 145133319-Oct-04 5:02 
QuestionHow to implement in ASP.NET? Pin
PhoneTech23-Feb-03 6:43
PhoneTech23-Feb-03 6:43 
I am programming a module for a CMS where you can add a file node to the base structure.

By adding the file node, you need to fill in a Physical Path where the files are located.

But the users cannot access those files from their clients so i need to create a virtual folder referring to this physical path.

I copied the iis manager class to my Business Logic Layer, and tried to create a new virtual folder.

But i get this problem:

Access is denied <br />
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. <br />
<br />
Exception Details: System.Runtime.InteropServices.COMException: Access is denied<br />
<br />
Source Error: <br />
<br />
<br />
Line 411:			public void CreateVirtualDirectory(string nameDirectory,string realPath)<br />
Line 412:			{	<br />
Line 413:				DirectoryEntry folderRoot = _iisServer.Children.Find("Root",VirDirSchemaName);<br />
Line 414:				try<br />
Line 415:				{<br />
 <br />
<br />
Source File: d:\visual studio project\bsu\components\filenode.cs    Line: 413 <br />
<br />
Stack Trace: <br />
<br />
<br />
[COMException (0x80070005): Access is denied]<br />
   System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)<br />
   System.DirectoryServices.DirectoryEntry.Bind()<br />
   System.DirectoryServices.DirectoryEntry.get_IsContainer()<br />
   System.DirectoryServices.DirectoryEntries.CheckIsContainer()<br />
   System.DirectoryServices.DirectoryEntries.Find(String name, String schemaClassName)<br />
   Components.IISManager.CreateVirtualDirectory(String nameDirectory, String realPath) in d:\visual studio project\bsu\components\filenode.cs:413<br />
   Components.FileNode.Add(Boolean CreateSubDirectoryNodes) in d:\visual studio project\bsu\components\filenode.cs:282<br />
   Site.FileActions.Add.ibSave_Click(Object sender, ImageClickEventArgs e) in d:\visual studio project\bsu\backsite\fileactions\add.aspx.cs:85<br />
   System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e)<br />
   System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)<br />
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)<br />
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)<br />
   System.Web.UI.Page.ProcessRequestMain()<br />
<br />
 <br />
<br />


I'm really stuk here! Could someone tell me how to correct this problem?
AnswerRe: How to implement in ASP.NET? Pin
Daron Yndem13-Jul-04 0:13
Daron Yndem13-Jul-04 0:13 
AnswerSolution for ASP.NET! Pin
Sire40417-Oct-04 23:20
Sire40417-Oct-04 23:20 
QuestionHow do you delete Virtual Directory? Pin
ahaddad20-Feb-03 8:18
ahaddad20-Feb-03 8:18 
AnswerRe: How do you delete Virtual Directory? Pin
Anonymous24-Jul-03 3:39
Anonymous24-Jul-03 3:39 
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 
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 

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.