Because all subdirectories have the <DIR>
field, you can easily differentiate subdirectories from files in the BuildDirectory()
function by looking for <DIR>
in each line:
//---Build the directory in the TreeView control---
private void BuildDirectory(TreeNode ParentNode) {
string[] listing = GetDirectoryListing(
Properties.Settings.Default.FTP_SERVER + ParentNode.FullPath);
foreach (string line in listing) {
if (line == String.Empty) break;
TreeNode node = new TreeNode();
if (line.Substring(24, 5) == '<DIR>') {
//---this is a directory; create a new node to be added---
node.Text = line.Substring(39);
node.ImageIndex = ico_CLOSE;
node.SelectedImageIndex = ico_OPEN;
//---add the dummy child node---
node.Nodes.Add('');
ParentNode.Nodes.Add(node);
} else {
//---this is a normal file; create a new node to be added---
node.Text = line.Substring(39);
node.ImageIndex = ico_PHOTO;
node.SelectedImageIndex = ico_PHOTO;
ParentNode.Nodes.Add(node);
}
}
}
When a node is selected, you first obtain its current path and then display that path in the status bar if it is a folder. If it is an image node, download and display the photo, using the DownloadImage()
function. All these are handled in the TreeView1_AfterSelect
event. Here's the code:
private void TreeView1_AfterSelect(object sender, TreeViewEventArgs e) {
//---always ignore the first '/' char---
string FullPath =
Properties.Settings.Default.FTP_SERVER +
e.Node.FullPath.Substring(1).Replace('
', '');
//---display the current folder selected---
if (e.Node.ImageIndex != ico_PHOTO) {
ToolStripStatusLabel1.Text = FullPath;
return;
}
//---download image---
DownloadImage(FullPath);
}
The DownloadImage()
function downloads an image from the FTP server and displays the image in a PictureBox control:
//---Download the image from the FTP server---
private void DownloadImage(string path) {
try {
ToolStripStatusLabel1.Text = 'Downloading image...' + path;
Application.DoEvents();
//---download the image---
FtpWebResponse FTPResp =
PerformWebRequest(path, WebRequestMethod.DownloadFile);
//---get the stream containing the image---
Stream ftpRespStream = FTPResp.GetResponseStream();
//---display the image---
PictureBox1.Image = Image.FromStream(ftpRespStream);
FTPResp.Close();
ToolStripStatusLabel1.Text =
'Downloading image...complete(' + path + ')';
} catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
To download an image file using FTP and then bind it to a PictureBox
control:
□ Call the PerformWebRequest()
helper function you defined earlier.
□ Retrieve the stream that contains response data sent from the FTP server, using the GetResponseStream()
method from the FtpWebResponse
class.
To set the PictureBox
control to display the downloaded image, use the FromStream()
method from the Image class to convert the response from the FTP server (containing the image) into an image.
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('
', '') +
@'/' + txtNewFolderName.Text;
//---make the new directory---
FtpWebResponse ftpResp =
PerformWebRequest(folder, WebRequestMethod.MakeDirectory);
ftpResp.Close();
//---refresh the newly added folder---
RefreshCurrentFolder();
//---update the statusbar---