Designing the Windows Forms Application
Start by launching Visual Studio and creating a new C# Windows Forms App project. Give it a clear name such as IISVirtualDirectoryCreator so the intent shows up at a glance. Once the project is in place, switch to the Designer view for the default form. The user interface is intentionally simple: it gathers the server address, the name of the virtual directory, the physical path that the directory should point to, and the type of directory (FTP or Web). A button initiates the creation process, and a label at the bottom displays the result.
Begin by adding a Label control for the server field and a matching TextBox named txtServer. Position the label to read “Server Name (or localhost):” and set its AutoSize property to true so the text never overlaps. The textbox will accept values such as localhost, a machine name, or an IP address. Place a second label and textbox pair for the virtual directory name; name the textbox txtVDirName. Add a third pair for the physical path; the textbox gets the name txtVDir. For the path you might type C:\Temp or any folder that the IIS user account can read.
Next, drop two RadioButton controls to let the user choose between FTP and Web. Group them by putting them inside a GroupBox named grpType with the caption “Virtual Directory Type”. Set the first radio button’s Text to “FTP Virtual Directory” and its Name to radioFTP. The second radio button gets the caption “Web Virtual Directory”, the name radioWeb, and set its Checked property to true so the Web option is selected by default.
The action button should be obvious. Drag a Button onto the form, set its Finally, add a Label at the bottom of the form to display status messages. Name it Arrange the controls neatly, spacing them consistently. Use the designer’s alignment tools to keep everything tidy. The resulting form should look like a small configuration panel, no more complicated than a typical settings dialog. To finish the UI, double‑click the form’s background in the Designer and add a Once the UI is set, you have a clean, functional foundation. The next step is to hook up the directory creation logic that interacts with IIS through the Directory Services API. The core of the application lies in the With the schema and root path established, build the Directory Services path by concatenating the IIS prefix, the user’s server address, and the chosen root. This yields a string like Next, add a child node to the root. Use When creating a Web virtual directory, the new entry also needs to be marked as an application. Invoke the Wrap the entire operation in a Here is the complete event handler in one place:Text property to “Create Virtual Directory”, and name it btnCreate. Under the Properties window, click the lightning bolt to open the events list, then double‑click the Click event to generate the handler btnCreate_Click
lblStatus, set AutoSize to true, and leave its Text blank initially. When the creation completes, the label will show success or error information.Form_Load event. In that method, you can pre‑populate the server field with Environment.MachineName so local testing becomes quick. You can also clear the status label on load to avoid stale messages.Implementing the Virtual Directory Logic
btnCreate_Click handler. The first task is to decide whether the user wants an FTP or a Web virtual directory. Each type uses a different schema class when creating the entry under the IIS metabase. For Web directories, the schema is IIsWebVirtualDir and the root path is /W3SVC/1/Root. For FTP directories, the schema becomes IIsFtpVirtualDir and the root path is /MSFTPSVC/1/Root. If the user leaves the radio buttons untouched, we default to the Web schema to avoid an empty state.IIS://localhost/W3SVC/1/Root. Create a DirectoryEntry object with that path; it represents the root node under which the new virtual directory will be added. Call RefreshCache on the entry to ensure you’re working with the latest data from the metabase.Children.Add with the user’s directory name and the schema you determined earlier. The new entry, stored in deNewVDir, is where you set the properties that tell IIS where to look on the file system. The Path property must match the physical folder you supplied in txtVDir. Insert this value at index 0 because the property is a collection.AppCreate method on the entry with a true argument. This tells IIS to treat the folder as a separate application boundary, which is often required for ASP.NET applications. After setting the properties, call CommitChanges on both the new entry and the root to write the changes back to IIS. Finally, close the entries to free up resources.try/catch block. If an exception occurs - whether due to insufficient permissions, a malformed path, or an existing directory with the same name - the catch clause updates lblStatus with the error message. On success, update the label to confirm that the directory was created, including the name and path for clarity.
Before you run the program, make sure the account executing the application has the rights to modify the IIS metabase. On recent Windows versions, this usually means running Visual Studio or the compiled executable as an administrator. If you prefer to avoid elevated privileges, you can grant the necessary permissions to a specific user account and run the application under that identity.
Testing is straightforward. Launch the application, enter localhost as the server, choose “Web Virtual Directory”, and set the name to TestApp with the path C:\Temp. Click the button and look at the status label. If successful, open the IIS Manager, expand Default Web Site, and verify that the new folder appears under the site with the correct physical path. The same procedure applies for FTP; just pick the FTP radio button, and IIS will create an FTP virtual directory instead.
This small tool demonstrates how Directory Services can automate IIS configuration. In deployment scripts or installation wizards, embedding similar logic saves time and reduces manual errors. Because the code is compact and self‑contained, it’s easy to adapt to larger projects that require dynamic configuration of multiple sites or directories.





No comments yet. Be the first to comment!