This chapter explained the parts that make up a .NET assembly. Splitting your application into multiple assemblies and modules will make your application easier to manage and update. At the same time, the CLR will only load the required assembly and modules, thereby making your application more efficient. If you have a shared assembly that can be used by other applications, consider deploying it into the Global Assembly Cache (GAC).
Part II
Application Development Using C#
Chapter 16
Developing Windows Applications
Chapters 16-19 show how you can use the C# language to create a different type of application. This chapter tackles Windows application development. The best way to learn a language is to actually work on a real project from the beginning to deployment. So, this chapter leads you through creating a Windows application that performs some useful tasks and then shows you how to deploy it using a technique in Visual Studio known as ClickOnce.
Specifically, the Windows application you build in this chapter demonstrates how to:
□ Programmatically access FTP servers using the FtpWebRequest
and FtpWebResponse
classes (both derived from the WebRequest
and WebResponse
classes in the System.Net
namespace)
□ Incorporate printing capability in your Windows application using the PrintDocument
class (located in the System.Drawing.Printing
namespace)
□ Deploy a Windows application using ClickOnce. You will also see how to programmatically cause an application to update itself.
The project in this chapter is a photo viewer Windows application that accesses an FTP server. Using this application, users can upload photos to an FTP server and also download and view images stored on the FTP server. The application is useful for companies that may need to access images uploaded by their partners. Insurance companies, for instance, may need to access photographs of car damage taken by auto body shop mechanics to facilitate estimating the cost of repair. Rather than build a complex web application, the shops and insurance companies can simply use this application to quickly upload and view photos. Users can also print the photos directly from the application.
Figure 16-1 shows how the application will look like when it is completed.
Figure 16-1
Configuring the FTP Server
Before you start writing the code of this application, you first need to configure FTP service for your computer. For this project, use the FTP service on your development machine.
By default, FTP service is not installed in Windows (note that FTP service is not available on Windows Vista Home editions). To add FTP Service to your computer, select Control Panel→Add or Remove Programs. Click the Add/Remove Windows Component tab, select Internet Information Services (IIS), and click the Details button. Select File Transfer Protocol (FTP) Service, and click OK.
To configure the FTP service on your computer, launch the Internet Information Services management console window by typing the command inetmgr
in the Run window. Your FTP site should look like Figure 16-2.
Figure 16-2
Right-click the Default FTP Site item, and select Properties. Click the Security Accounts tab. Ensure that the Allow Anonymous Connections checkbox is checked (see Figure 16-3) to enable an anonymous user to log in to your FTP service.
Figure 16-3
Next, click on the Home Directory tab, and check the Write checkbox (see Figure 16-4). This allows users to your FTP service to upload files and create directories on the FTP server.
Figure 16-4
Click OK to finish the configuration of the FTP service.
Using Visual Studio 2008, create a new Windows application and name it PhotoViewer
. Populate the default Form1
with the controls shown in Figure 16-5. These controls are:
Control |
Text |
Name |
Button controls (4) |
Create Folder |
btnCreateFolder |
Remove Folder |
btnRemoveFolder |
Upload Photos |
btnUploadPhotos |
Delete Photo |
btnDeletePhoto |
GroupBox controls (3) |
FTP Server |
|
Folders |
|
Photos |
|
Label controls (6) |
Server Name/IP |
|
User Name |
|
Password |
|
Select folder |
|
New folder name |
|
Selected Photo |
|
PictureBox |
|
PictureBox1 |
TextBox controls (4) |
|
txtFTPServer |
|
txtUserName |
|
txtPassword |
|
txtNewFolderName |
ToolStripStatusLabel |
ToolStripStatusLabel1 |
ToolStripStatusLabel1 |
TreeView |
|
TreeView1 |
Figure 16-5
The source code for this project can be downloaded from Wrox's web site at www.wrox.com.
You'll also need to add an ImageList
control ( ImageList1
) to Form1
to contain three images representing an opened folder, a closed folder, and an image file. You can specify these images in the control's Image
property (see Figure 16-6).
Figure 16-6
Set the control properties in the following table.
Control |
Property |
Value |
TreeView1 |
ImageList |
ImageList1 |
PictureBox1 |
SizeMode |
Zoom |
txtPassword |
PasswordChar |
"*" |
Using Application Settings
When users launch the PhotoViewer
application, they need to supply three pieces of information to access the FTP Server:
Читать дальше