Creat FTP or WEb IIS Virtual Directory Using C#

來源:互聯網
上載者: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 button1_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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.