Книга: C# 2008 Programmer

Creating a New Directory

Creating a New Directory

The user can create a new directory on the FTP server by clicking the Create Folder button. To create a new directory, select a node (by clicking on it) to add the new folder, and then call the PerformWebRequest() helper function you defined earlier. This is accomplished by the Create Folder button:

//---Create a new folder---
private void btnCreateFolder_Click(object sender, EventArgs e) {
 //---ensure user selects a folder---
 if (TreeView1.SelectedNode.ImageIndex == ico_PHOTO) {
  MessageBox.Show("Please select a folder first.");
  return;
 }
 try {
  //---formulate the full path for the folder to be created---
  string folder = Properties.Settings.Default.FTP_SERVER +
   TreeView1.SelectedNode.FullPath.Substring(1).Replace("r", "") +
   @"/" + txtNewFolderName.Text;
  //---make the new directory---
  FtpWebResponse ftpResp =
   PerformWebRequest(folder, WebRequestMethod.MakeDirectory);
  ftpResp.Close();
  //---refresh the newly added folder---
  RefreshCurrentFolder();
  //---update the statusbar---
  ToolStripStatusLabel1.Text =
   ftpResp.StatusDescription.Replace("rn",string.Empty);
 } catch (Exception ex) {
  MessageBox.Show(ex.ToString());
 }
}

When a new folder is created, you update the TreeView control to reflect the newly added folder. This is accomplished by the RefreshCurrentFolder() function:

private void RefreshCurrentFolder() {
 //---clears all the nodes and...---
 TreeView1.SelectedNode.Nodes.Clear();
 //---...create the nodes again---
 BuildDirectory(TreeView1.SelectedNode);
}

Оглавление книги


Генерация: 1.110. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз