Posted on 2009-07-15 10:59
Hero 阅读(370)
评论(0) 编辑 收藏 引用 所属分类:
C#积累
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.IO;
9 using System.Net;
10
11 namespace FtpClient
12 {
13 public partial class FtpClient : Form
14 {
15 //创建私有成员
16 private string serverDirectory;
17
18 public FtpClient()
19 {
20 InitializeComponent();
21 }
22
23 //填充listbox
24 private void FillDirectoryList( Stream stream )
25 {
26 StreamReader reader = new StreamReader( stream );
27 string content = reader.ReadToEnd();
28 string[] files = content.Split( '\n' );
29
30 //设定数据源
31 listBox_ListFiles.DataSource = files;
32 reader.Close();
33 }
34
35 private void FtpClient_Load( object sender, EventArgs e )
36 {
37
38 }
39
40 private void button_Open_Click( object sender, EventArgs e )
41 {
42 Cursor currentCursor = this.Cursor;
43 FtpWebResponse response = null;
44 Stream stream = null;
45
46 try
47 {
48 //暂存光标位置
49 this.Cursor = Cursors.WaitCursor;
50
51 //////////////////////////////////////////////////////////////////////////
52 /*
53 * 创建FTP对象
54 */
55
56 //创建对服务器的请求
57 FtpWebRequest request = (FtpWebRequest)WebRequest.Create( this.textBox_Server.Text );
58 //创建网络验证
59 request.Credentials = new NetworkCredential( this.textBox_UserName.Text, this.textBox_Password.Text );
60 //设置请求方法
61 request.Method = WebRequestMethods.Ftp.ListDirectory;
62
63
64 /*
65 * 发送请求到服务器
66 */
67 response = (FtpWebResponse)request.GetResponse();
68
69 /*
70 * 读取响应并且填充Listbox
71 */
72 stream = response.GetResponseStream();
73 FillDirectoryList( stream );
74
75
76
77 serverDirectory = null;
78 button_GetFile.Enabled = false;
79 button_OpenDirectory.Enabled = false;
80 }
81 catch (System.Exception ex)
82 {
83 MessageBox.Show( ex.Message, "Error FTP Client", MessageBoxButtons.OK, MessageBoxIcon.Error );
84 }
85 finally
86 {
87 if ( response != null ) response.Close();
88 if ( stream != null ) stream.Close();
89
90 this.Cursor = currentCursor;
91 }
92 }
93
94 private void button_OpenDirectory_Click( object sender, EventArgs e )
95 {
96 FtpWebResponse response = null;
97 Stream stream = null;
98
99 try
100 {
101 //获得子目录
102 string subDirectory = this.listBox_ListFiles.SelectedValue.ToString().Trim();
103 serverDirectory += "/" + subDirectory;
104
105 Uri baseUri = new Uri( this.textBox_Server.Text );
106 Uri uri = new Uri( baseUri, serverDirectory );
107
108 //WebRequest是抽象类,不能直接使用
109 FtpWebRequest request = (FtpWebRequest)WebRequest.Create( uri );
110 request.Credentials = new NetworkCredential( this.textBox_UserName.Text, this.textBox_Password.Text );
111 request.Method = WebRequestMethods.Ftp.ListDirectory;
112
113 response = (FtpWebResponse)request.GetResponse();
114 stream = response.GetResponseStream();
115 this.FillDirectoryList( stream );
116 }
117 catch (System.Exception ex)
118 {
119 MessageBox.Show( ex.Message, "Error FTP Client", MessageBoxButtons.OK, MessageBoxIcon.Error );
120 }
121 finally
122 {
123 if ( response != null ) response.Close();
124 if ( stream != null ) stream.Close();
125 }
126 }
127
128 private void button_GetFile_Click( object sender, EventArgs e )
129 {
130 FtpWebResponse response = null;
131 Stream instream = null;
132 Stream outstream = null;
133
134 try
135 {
136 Uri baseuri = new Uri( this.textBox_Server.Text );
137
138 string filename = this.listBox_ListFiles.SelectedValue.ToString().Trim();
139 string fullFileName = serverDirectory + "/" + filename;
140
141 Uri uri = new Uri( baseuri, fullFileName );
142
143 FtpWebRequest request = (FtpWebRequest)WebRequest.Create( uri );
144 request.Credentials = new NetworkCredential( this.textBox_UserName.Text, this.textBox_Password.Text );
145 request.Method = WebRequestMethods.Ftp.ListDirectory;
146
147 response = (FtpWebResponse)request.GetResponse();
148 //获得文件流
149 instream = response.GetResponseStream();
150 this.saveFileDialog_location.FileName = filename;
151
152 if ( saveFileDialog_location.ShowDialog() == DialogResult.OK )
153 {
154 outstream = File.OpenWrite( saveFileDialog_location.FileName );
155 byte[] buffer = new byte[4096];
156 int size = 0;
157 while( (size=instream.Read(buffer, 0, 4096 )) > 0 )
158 {
159 outstream.Write( buffer, 0, size );
160 }
161 }
162 }
163 catch (System.Exception ex)
164 {
165 MessageBox.Show( ex.Message );
166 }
167 finally
168 {
169 if ( instream != null ) instream.Close();
170 if ( outstream != null ) outstream.Close();
171 if ( response != null ) response.Close();
172 }
173 }
174
175 private void listBox_ListFiles_SelectedIndexChanged( object sender, EventArgs e )
176 {
177 this.button_GetFile.Enabled = true;
178 this.button_OpenDirectory.Enabled = true;
179 }
180 }
181 }