Click here to Skip to main content
12,401,158 members (61,083 online)
Click here to Skip to main content
Add your own
alternative version

Stats

86.8K views
1.9K downloads
49 bookmarked
Posted

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.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 3 Pin
agzis200819-Aug-10 5:53
memberagzis200819-Aug-10 5:53 
GeneralRoot Directory Pin
Aazzin10-Apr-08 2:25
memberAazzin10-Apr-08 2:25 
GeneralExample systray app to create/edit virtual directories Pin
MonkeyBumps30-Nov-07 3:57
memberMonkeyBumps30-Nov-07 3:57 
GeneralUnknown error (0x80005000) [modified] Pin
Bigbirddk23-May-07 10:56
memberBigbirddk23-May-07 10:56 
Questionremote IIS6.0 Administration using c# Pin
manubee5-May-07 2:25
membermanubee5-May-07 2:25 
GeneralCannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7) [modified] Pin
ureyes8419-Apr-07 12:45
memberureyes8419-Apr-07 12:45 
GeneralRe: Cannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7) Pin
ureyes846-Aug-07 11:27
memberureyes846-Aug-07 11:27 
GeneralRe: Cannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7) Pin
coolshad9-Apr-09 7:03
membercoolshad9-Apr-09 7:03 
QuestionHow to set user and password for FTP directory during creation? Pin
dliebich8-Apr-07 0:52
memberdliebich8-Apr-07 0:52 
AnswerRe: How to set user and password for FTP directory during creation? Pin
Palwinder Singh9-Apr-07 22:42
memberPalwinder Singh9-Apr-07 22:42 
QuestionRe: How to set user and password for FTP directory during creation? Pin
dliebich9-Apr-07 22:50
memberdliebich9-Apr-07 22:50 
QuestionGreat article- redirect to a url anyone? [modified] Pin
johnPH26-Mar-07 7:53
memberjohnPH26-Mar-07 7:53 
AnswerRe: Great article- redirect to a url anyone? Pin
johnPH28-Mar-07 5:27
memberjohnPH28-Mar-07 5:27 
QuestionHow to create a virtual directory on other website Pin
breakpoint22-Jan-07 18:16
memberbreakpoint22-Jan-07 18:16 
GeneralOther settings Pin
homerbush30-Nov-06 8:29
memberhomerbush30-Nov-06 8:29 
QuestionSystem.Runtime.InteropServices.COMException: Access is denied Pin
farajallah9829-Mar-06 20:46
memberfarajallah9829-Mar-06 20:46 
AnswerRe: System.Runtime.InteropServices.COMException: Access is denied Pin
Palwinder Singh17-May-06 1:43
memberPalwinder Singh17-May-06 1:43 
GeneralNice work Pin
fatih isikhan7-Feb-06 20:15
memberfatih isikhan7-Feb-06 20:15 
GeneralRe: Nice work Pin
Palwinder Singh9-Feb-06 0:48
memberPalwinder Singh9-Feb-06 0:48 

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.

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