Create a New Visual C # Windows Forms project and add controls on the default form as shown below.
Get devnewz newsletter free-"> Click here |
Figure 1: User Interface The Windows form allows the user to specify the server name on which to create the virtual directory, the virtual directory name, the virtual directory path, the type of virtual directory (ftp or Web ). we will now add the code to create the virtual directory when the button is clicked. the code is specified in the Code listing below. we first determine if the user has selected to create an FTP virtual directory or a Web virtual directory. based on this selection, we set the values for the schema name and a part of the path for the IIS root to create the node in. we now create a directoryentry object for the root of the IIS directory on the server specified by the user which points to the Web or FTP root. we then add a node to the children collection of the root node and set the value for the path property of the newly created node. if the user selected to create a web virtual directory, we set the new node as an application. we then commit the changes and close the Directory Entry nodes. finally we display the status-whether successful or error, to the user. you need to make sure that the user has the privileges to create the new virtual directories before running the sample. Private void button#click (Object sender, system. eventargs E) { String strschema = ""; String strrootsubpath = ""; If (radiobutton1.checked) { Strschema = "iiswebvirtualdir "; Strrootsubpath = "/w3svc/1/root "; } If (radiobutton2.checked) { Strschema = "iisftpvirtualdir "; Strrootsubpath = "/msftpsvc/1/root "; } If (strschema = "") { Strschema = "iiswebvirtualdir "; Strrootsubpath = "/w3svc/1/root "; } Directoryentry deroot = new directoryentry ("IIS: //" + txtserver. Text + strrootsubpath ); Try { Deroot. refreshcache (); Directoryentry denewvdir = deroot. Children. Add (txtvdirname. Text, strschema ); Denewvdir. properties ["path"]. insert (0, txtvdir. Text ); Denewvdir. commitchanges (); Deroot. commitchanges (); // Create a application If (strschema = "iiswebvirtualdir ") Denewvdir. Invoke ("appcreate", true ); // Save changes Denewvdir. commitchanges (); Deroot. commitchanges (); Denewvdir. Close (); Deroot. Close (); Lblstatus. Text = "virtual directory" + txtvdirname. Text + "(" + txtvdir. Text + ") has been created "; } Catch (exception ex) { Lblstatus. Text = "error:" + ex. message; } }
Code listing: button click event handler code-create a new virtual directory. Please refer to the complete code listing available for download at the top of the article. Figure: creating a virtual directory from our program. This creates a FTP virtual directory named "testftpdir" pointing to path "C: \ Temp" Conclusion: In this article, we saw how to create FTP and web virtual directories programmatically. This can be very useful in deployment scenarios. * Origninally published at csharpcorner |