ToolStripStatusLabel1.Text =
ftpResp.StatusDescription.Replace("\r\n", string.Empty);
} catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
If a directory is not empty (that is, if it contains files and subdirectories), the deletion process will fail. The user will have to remove its content before removing the directory.
To upload photos to the FTP server, you first select a folder to upload the photos to and then use the OpenFileDialog
class to ask the user to select the photo(s) he wants to upload. Finally, you upload the photos individually, using the UploadImage()
function:
private void btnUploadPhotos_Click(object sender, EventArgs e) {
//---ensure user selects a folder---
if (TreeView1.SelectedNode.ImageIndex == ico_PHOTO) {
MessageBox.Show("Please select a folder to upload the photos.");
return;
}
OpenFileDialog openFileDialog1 = new OpenFileDialog() {
Filter = "jpg files (*.jpg)|*.jpg",
FilterIndex = 2,
RestoreDirectory = true,
Multiselect = true
};
//---formulate the full path for the folder to be created---
string currentSelectedPath =
Properties.Settings.Default.FTP_SERVER +
TreeView1.SelectedNode.FullPath.Substring(1).Replace("\r", "");
//---let user select the photos to upload---
if (openFileDialog1.ShowDialog() ==
System.Windows.Forms.DialogResult.OK) {
//---upload each photo individually---
for (int i = 0; i <= openFileDialog1.FileNames.Length - 1; i++) {
UploadImage(currentSelectedPath + "/" +
openFileDialog1.FileNames[i].Substring(
openFileDialog1.FileNames[i].LastIndexOf(@"\") + 1),
openFileDialog1.FileNames[i]);
}
}
//---refresh the folder to show the uploaded photos---
RefreshCurrentFolder();
}
The UploadImage()
function uploads a photo from the hard disk to the FTP server:
□ First, create a new instance of the WebClient
class.
□ Specify the login credential to the FTP server.
□ Upload the file to the FTP server, using the UploadFile()
method from the WebClient
class. Note that the full pathname of the file to be uploaded to the FTP server must be specified.
//---upload a photo to the FTP server---
private void UploadImage(string path, string filename) {
try {
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(
Properties.Settings.Default.UserName,
Properties.Settings.Default.Password);
//---upload the photo---
client.UploadFile(path, filename);
//---update the statusbar---
ToolStripStatusLabel1.Text = filename + " uploaded!";
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
To delete a photo, the user first selects a photo to delete and then you call the PerformWebRequest()
helper function you have defined earlier:
private void btnDeletePhoto_Click(object sender, EventArgs e) {
if (TreeView1.SelectedNode.ImageIndex != ico_PHOTO) {
MessageBox.Show("Please select a photo to delete.");
return;
} try {
string FullPath = Properties.Settings.Default.FTP_SERVER +
TreeView1.SelectedNode.FullPath.Substring(1).Replace("\r", "");
//---delete the photo---
FtpWebResponse ftpResp =
PerformWebRequest(FullPath, WebRequestMethod.DeleteFile);
//---delete the current node---
TreeView1.SelectedNode.Remove();
//---update the statusbar---
ToolStripStatusLabel1.Text =
ftpResp.StatusDescription.Replace("\r\n", string.Empty);
} catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
Once the photo is removed from the FTP server, you also need to delete its node in the TreeView
control.
That's it! You can now test the application by pressing F5. Ensure that the credentials for logging in to the FTP server are correct. If the login is successful, you should be able to create a new folder on the FTP server and then upload photos. Figure 16-12 shows the complete application.
Figure 16-12
The .NET Framework contains classes that make it easy for you to support printing in your applications. In this section, you add printing support to the PhotoViewer application so that you can print the photos. You'll explore the basics of printing in .NET and see how to configure page setup, print multiple pages, and preview a document before it is printed, as well as let users select a printer with which to print.
Basics of Printing in .NET
In .NET, all the printing functionality is encapsulated within the PrintDocument
control/class, which can be found in the Toolbox (see Figure 16-13). The PrintDocument
control defines the various methods that allow you to send output to the printer.
Figure 16-13
To incorporate printing functionality into your Windows application, you can either drag and drop the PrintDocument
control onto your form or create an instance of the PrintDocument
class at runtime. This example uses the latter approach.
To start the printing process, you use the Print()
method of the PrintDocument
class. To customize the printing process using the PrintDocument
object, there are generally three events with which you need to be acquainted:
Читать дальше