Creat FTP or web IIS virtual directory using C #

Source: Internet
Author: User
In this example we will create a Windows form project that will create new FTP and web IIS virtual directories from code based on the name and path specified by the user. you can create virtual directories on the local computer by specifying the server name as "localhost" or you can create the virtual directory on a remote computer by specifying the machine name.

We make use of the directoryservices namespace for creating the virtual directory.

Step 1: Create the project and the basic user interface.

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.