Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Creating FTP and Web IIS virtual directories

0.00/5 (No votes)
5 Feb 2006 2  
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