Click here to Skip to main content
13,198,563 members (41,776 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 16 Nov 2006

Allow user to set physical directory path during Web Project deployment

, 16 Nov 2006
Rate this:
Please Sign up or sign in to vote.
How to deploy a Web application to a user defined physical directory.


We want the user to set the physical path for our Web application IIS virtual directory, during deployment. Since the Web Setup project does not allow this, our idea is to use the Application Setup project to create the physical path, and content, and later with a Custom Action, create the virtual directory, using IIS Admin Objects.


In a previous article posted, we talked about how to set the IIS Virtual Directory Authentication Settings. Take a look at the article if you also want to change this.

Using the code

Start by creating a new Web Application project, maybe you have already one to deploy. Create a new Web Form and name it Default.aspx.

Now, create a new Visual Basic Class Library called CustomAction. Delete Class1.vb. Add a new class, based on the Installer Class template, named CustomAction.vb to the project. (Right click Add->Add class... Select Installer Class from templates). Open it and switch to Code View.


Imports System.IO

to the CustomAction.vb class Imports section.

Now add the following method overrides for the installer class:

This is the method called after the Setup Deployment Project installer predefined stuff. It creates the new virtual directory in the default IIS server, set its physical path to the application folder, creates the Web application as In Process, and sets the default document page. It also shows hot to save the installer state to be read during uninstall:

Public Overrides Sub Install(ByVal stateSaver As _
    ' Execute inherited Install

    ' Get Assembly
    Dim Asm As System.Reflection.Assembly = _

    Dim DirLocation As String = Path.GetDirectoryName(Asm.Location)

    REM *******************************************************
    REM Create IIS Virtual Dir 

    ' TODO: Search for the Server Number <-------¬
    Dim vRoot = GetObject("IIS://LocalHost/W3svc/1/Root")

    ' Create de Virtual Dir
    Dim vDir = vRoot.Create("IIsWebVirtualDir", _

    ' Set the Physical Path for the virtual Dir
    vDir.Path = DirLocation

    ' Set the default Doc
    vDir.DefaultDoc = "Default.aspx"

    ' Actually set Info

    'Create an application in-process. 

    ' Save the Virtual Dir
    stateSaver.Add("VirtualDir", _

End Sub

This is the method called after the Setup Deployment Project unnistaller predefined stuff. It gets the VirtualDir key value saved in the installer state, and uses it to delete the virtual directory from IIS during un-installation:

' Override 'Uninstall' method of Installer class.
Public Overrides Sub Uninstall(ByVal mySavedState As IDictionary)
        ' Get virtual dir
        Dim vRoot = GetObject("IIS://LocalHost/W3svc/1/Root")

        ' Delete Virtual Dir
        vRoot.Delete("IIsWebVirtualDir", mySavedState("VirtualDir"))

    Catch ex As Exception

    End Try
End Sub 'Uninstall

And this is the method called after all the setup deployment steps are executed, it opens the Web application default page in Internet Explorer:

' Override the 'Commit' method of the Installer class.
Public Overrides Sub Commit(ByVal mySavedState As IDictionary)
    ' Open the virtual dir after install
        Dim P As New Process
        P.Start("iexplore.exe", "http://localhost/" & _
                Me.Context.Parameters.Item("dir") & "/")
    Catch ex As Exception

    End Try
End Sub 'Commit

Save CustomAction.vb. Now, add the "Web To Install" Setup Project to the solution. (Note, we use an Application Setup Project, not a Web Setup Project, for this deployment.)

Add the Primary Output and Content Files of the WebToIstall project to the Setup Project output. (Right click -> Add -> Project output...)

Repeat the operation to add the CustomAction project Primary Output.

Add the CustomActions custom actions to the Setup Project:

  • Install: Right click -> View -> Custom Actions. In the Custom Actions view, right click Install -> Add Custom Action. Double click "Application Folder". Select CustomAction Primary Output. Click OK. Right click the new custom action -> Properties window. Set the CustomActionData property to "/dir=[VIRTDIR]" (without the ""s).
  • Commit: Right click -> View -> Custom Actions. In the Custom Actions view, right click Commit-> Add Custom Action. Double click "Application Folder". Select CustomAction Primary Output. Click OK. Right click the new custom action -> Properties window. Set the CustomActionData property to "/dir=[VIRTDIR]" (without the ""s).
  • Uninstall: Right click -> View -> Custom Actions. In the Custom Actions view, right click Uninstall-> Add Custom Action. Double click "Application Folder". Select CustomAction Primary Output. Click OK.

Now, to add the virtual directory input textbox interface:

In Solution Explorer, right click the Setup Project -> View -> User Interface. Right click Install Start section -> Add dialog. Select Textboxes (A), click OK. Drag the new dialog just before the Welcome dialog. Right click new dialog -> Properties window. Set the following properties (without the ""s): BannerText to "Virtual Directory Name", BodyText to "Specify the Virtual Directory Name", Edit1Label to "Virtual Dir:", Edit1Property to "VIRTDIR", Edit1Value to "WebToInstall", or specify your predefined virtual directory name. Set Edit2Visible, Edit3Visible, and Edit4Visible to "False".

Points of Interest

That's it! This code also shows how to keep track of installer data, using the installer state. And that sometimes when we get stuck, trying to do things only using one way, when there is a more easy one, which can be seen if we only just look things from the other way around.


  • Nov. 16, 2006 - Initial version.


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


About the Author

Software Developer CIMEX S.A.
Cuba Cuba
Rodolfo Ortega is a Cuban Computer Scientist. He works as IT Auditor for the CIMEX S.A. subsidiary in Holguin, Cuba. He lives and works in Holguin, in the eastern part of the island of Cuba.

You can contact him at rodolfom[] for any personal message: Ideas on new articles, bibliography about new APIs, questions, are wellcome.

Submit questions related with current article to the article forum.

You may also be interested in...


Comments and Discussions

Generalim installing a wcf service Pin
sfsddf11-Sep-12 20:46
membersfsddf11-Sep-12 20:46 
GeneralGreat Article! One question... Pin
ma9169-Nov-10 3:14
memberma9169-Nov-10 3:14 
GeneralMy vote of 4 Pin
Balaji_Bala26-Jul-10 22:52
memberBalaji_Bala26-Jul-10 22:52 
GeneralPhysical Path [modified] Pin
Shakeel Ahmed Awan24-Sep-08 2:37
memberShakeel Ahmed Awan24-Sep-08 2:37 
Generalalternative Pin
moin0215-Dec-06 5:24
membermoin0215-Dec-06 5:24 
GeneralRe: alternative Pin
jeff reece6-Dec-06 15:03
memberjeff reece6-Dec-06 15:03 
GeneralRe: alternative Pin
moin0217-Dec-06 16:40
membermoin0217-Dec-06 16:40 
GeneralRe: alternative Pin
rmortega777-Dec-06 9:00
memberrmortega777-Dec-06 9:00 
QuestionWhat is a server number? Pin
Carmela Pham29-Nov-06 9:49
memberCarmela Pham29-Nov-06 9:49 
AnswerRe: What is a server number? Pin
rmortega7730-Nov-06 5:39
memberrmortega7730-Nov-06 5:39 
GeneralRe: What is a server number? Pin
Carmela Pham30-Nov-06 8:21
memberCarmela Pham30-Nov-06 8:21 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 16 Nov 2006
Article Copyright 2006 by rmortega77
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid