Click here to Skip to main content
Click here to Skip to main content

Creating FTP and Web IIS virtual directories

, 5 Feb 2006
Rate this:
Please Sign up or sign in to vote.
Creating FTP and Web IIS virtual directories programatically, using C#.

Sample Image - VirtualDirectoryCreation.gif

Introduction

This article would give you a start for creating a FTP or Web IIS virtual directory in the Internet Information Server. When I was working on the deployment of a web solution on the .NET platform, I was surprised that I was not able to customize the installer for providing a different physical path rather than the IIS root directory for security reasons. So I wrote a different installer class and customized the installations by prompting the user for the name of the virtual directory and the path where to install the source files or the directory to map with the virtual directory. This class provides the two methods for creating and deleting a virtual directory. Initially I wrote this for IIS web directories, then later on I extended it for FTP virtual directories.

Using the code

In order to use the class, add a reference to the DLLL System.DirectoryServices and import the namespace VirtualDirectoryCreation in your class.

Then create an object of the VdirLib class. This class contains four properties and an enum type.

The enum VDirType holds the type of the virtual directory to be created. That can be a FTP virtual directory or a Web IIS virtual directory.

public enum VDirType
{
    FTP_DIR, WEB_IIS_DIR
};

The property strDirectoryType of type enum VDirType returns or sets the virtual directory to be created. That could be a FTP or a Web IIS virtual directory.

private VDirType _strDirectoryType;

public VDirType strDirectoryType
{
    get
    {
        return _strDirectoryType;
    }
    set
    {
        _strDirectoryType = value;
    }
}

Property strPhysicalPath gets / sets the physical path that would be mapped to the virtual directory.

private string _strPhysicalPath;
       
public string strPhysicalPath
{
    get
    {
        return _strPhysicalPath;
    }
    set
    {
        _strPhysicalPath = value;
    }
}

Property strVDirName gets / sets the name of the virtual directory to be created.

private string _strVDirName;
        
public string strVDirName
{
    get
    {
        return _strVDirName;
    }
    set
    {
        _strVDirName = value;
    }
}

Property strServerName gets / sets the name of the server where to create the virtual directory.

private string _strServerName;

public string strServerName
{
    get
    {
        return _strServerName;
    }
    set
    {
        _strServerName = value;
    }

}

The function CreateVDir() creates the virtual directory and returns the success string, or an error message in the case of an exception.

public string CreateVDir()
{
    System.DirectoryServices.DirectoryEntry oDE;
    System.DirectoryServices.DirectoryEntries oDC;
    System.DirectoryServices.DirectoryEntry oVirDir;
    try
    {
        //check whether to create FTP or Web IIS Virtual Directory
        if (this._strDirectoryType == VDirType.WEB_IIS_DIR)
        {
            oDE = new DirectoryEntry("IIS://" + 
                  this._strServerName + "/W3SVC/1/Root");
        }
        else
        {
            oDE = new DirectoryEntry("IIS://" + 
                  this._strServerName + "/MSFTPSVC/1/Root");
        }
        
        //Get Default Web Site
        oDC = oDE.Children;

        //Add row
        oVirDir = oDC.Add(this._strVDirName, 
                  oDE.SchemaClassName.ToString());
        
        //Commit changes for Schema class File
        oVirDir.CommitChanges();

        //Create physical path if it does not exists
        if (!Directory.Exists(this._strPhysicalPath))
        {
            Directory.CreateDirectory(this._strPhysicalPath);
        }

        //Set virtual directory to physical path
        oVirDir.Properties["Path"].Value = this._strPhysicalPath;

        //Set read access
        oVirDir.Properties["AccessRead"][0] = true;

        //Create Application for IIS Application (as for ASP.NET)
        if (this._strDirectoryType == VDirType.WEB_IIS_DIR)
        {
            oVirDir.Invoke("AppCreate", true);
            oVirDir.Properties["AppFriendlyName"][0] = this._strVDirName;
        }
        
        //Save all the changes
        oVirDir.CommitChanges();

        return "Virtual Directory created sucessfully";

    }
    catch (Exception exc)
    {
        return exc.Message.ToString();
    }
}

The function DeleteVDir() deletes the virtual directory and returns the success string, or an error message in the case of an exception.

public string DeleteVDir()
{
    System.DirectoryServices.DirectoryEntry oDE;
    System.DirectoryServices.DirectoryEntries oDC;
    try
    {
        //check whether to delete FTP or Web IIS Virtual Directory
        if (this._strDirectoryType == VDirType.WEB_IIS_DIR)
        {
            oDE = new DirectoryEntry("IIS://" + 
                  this._strServerName + "/W3SVC/1/Root");
        }
        else
        {
            oDE = new DirectoryEntry("IIS://" + 
                      this._strServerName + "/MSFTPSVC/1/Root");
        }
        oDC = oDE.Children;
        
        //Find and remove the row from Directory entry.
        oDC.Remove(oDC.Find(this._strVDirName, 
             oDE.SchemaClassName.ToString()));
        
        //Save the changes
        oDE.CommitChanges();

        return "Virtual Directory deleted sucessfully";

    }
    catch (Exception exc)
    {
        return exc.Message.ToString();
    }
}

In order to create a virtual directory, set the four properties strDirectoryType, strPhysicalPath, strVDirName, and strServerName to the name of your server, and call the function CreateVDir(). In the same way, you can delete the existing virtual directory by calling the function DeleteVDir() after setting these properties of the class VdirLib.

Conclusion

This article explains how to create a FTP or Web IIS virtual directory in the Internet Information Server.

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

Share

About the Author

Palwinder Singh
Team Leader
India India
Techinical Lead on Microsoft technologies.Also,worked with PHP/ MYSQL. My core expertise is on dot net framework 2.0,3.5 and 4.0.

Comments and Discussions

 
GeneralMy vote of 3 Pinmemberagzis200819-Aug-10 6:53 
GeneralRoot Directory PinmemberAazzin10-Apr-08 3:25 
GeneralExample systray app to create/edit virtual directories PinmemberMonkeyBumps30-Nov-07 4:57 
GeneralUnknown error (0x80005000) [modified] PinmemberBigbirddk23-May-07 11:56 
Questionremote IIS6.0 Administration using c# Pinmembermanubee5-May-07 3:25 
GeneralCannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7) [modified] Pinmemberureyes8419-Apr-07 13:45 
GeneralRe: Cannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7) Pinmemberureyes846-Aug-07 12:27 
GeneralRe: Cannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7) Pinmembercoolshad9-Apr-09 8:03 
QuestionHow to set user and password for FTP directory during creation? Pinmemberdliebich8-Apr-07 1:52 
AnswerRe: How to set user and password for FTP directory during creation? PinmemberPalwinder Singh9-Apr-07 23:42 
QuestionRe: How to set user and password for FTP directory during creation? Pinmemberdliebich9-Apr-07 23:50 
QuestionGreat article- redirect to a url anyone? [modified] PinmemberjohnPH26-Mar-07 8:53 
AnswerRe: Great article- redirect to a url anyone? PinmemberjohnPH28-Mar-07 6:27 
QuestionHow to create a virtual directory on other website Pinmemberbreakpoint22-Jan-07 19:16 
GeneralOther settings Pinmemberhomerbush30-Nov-06 9:29 
QuestionSystem.Runtime.InteropServices.COMException: Access is denied Pinmemberfarajallah9829-Mar-06 21:46 
AnswerRe: System.Runtime.InteropServices.COMException: Access is denied PinmemberPalwinder Singh17-May-06 2:43 
GeneralNice work Pinmemberfatih isikhan7-Feb-06 21:15 
GeneralRe: Nice work PinmemberPalwinder Singh9-Feb-06 1:48 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 5 Feb 2006
Article Copyright 2006 by Palwinder Singh
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid