注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

淡忘邻人

二十七,始发愤

 
 
 

日志

 
 
 
 

FTP下载  

2010-05-16 11:51:14|  分类: C# |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;


namespace WindowsFormsApplication1
{
    public partial class FtpClientForm : Form
    {
        public FtpClientForm()
        {
            InitializeComponent();
        }

        private string serverDirectory;

        private void FillDirectoryList(Stream stream)
        {
            StreamReader reader = new StreamReader(stream);
            string content = reader.ReadToEnd();
            string[] files = content.Split('\n');
            listFiles.DataSource = files;
            reader.Close();
        }

        private void buttonOpen_Click(object sender, EventArgs e)
        {
            Cursor currentCursor = this.Cursor;
            FtpWebResponse response = null;
            Stream stream = null;

            try
            {
                this.Cursor = Cursors.WaitCursor;
                //create the ftpwebrequest object.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(textServer.Text);
                request.Credentials = new NetworkCredential(textUsername.Text, textPassword.Text);
                request.Method = WebRequestMethods.Ftp.ListDirectory;
                //send the request to the server.
                response = (FtpWebResponse)request.GetResponse();
                //read the response and fill the list box.
                stream = response.GetResponseStream();
                FillDirectoryList(stream);

                serverDirectory = null;
                buttonOpenDirectory.Enabled = true;
                buttonGetFile.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (response != null)
                    response.Close();
                if (stream != null)
                    stream.Close();
                this.Cursor = currentCursor;
            }
        }

        private void buttonOpenDirectory_Click(object sender, EventArgs e)
        {
            FtpWebResponse response = null;
            Stream stream = null;

            try
            {
                string subDirectory = listFiles.SelectedValue.ToString().Trim();
                serverDirectory += "/" + subDirectory;
                Uri baseUri = new Uri(textServer.Text);
                Uri uri = new Uri(baseUri, serverDirectory);

                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
                request.Credentials = new NetworkCredential(textUsername.Text, textPassword.Text);

                request.Method = WebRequestMethods.Ftp.ListDirectory;
                response = (FtpWebResponse)request.GetResponse();

                stream = response.GetResponseStream();
                FillDirectoryList(stream);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (response != null)
                    response.Close();
                if (stream != null)
                    stream.Close();
            }
        }

        private void buttonGetFile_Click(object sender, EventArgs e)
        {
            FtpWebResponse response = null;
            Stream inStream = null;
            Stream outStream = null;
            try
            {
                Uri baseUri = new Uri(textServer.Text);

                string filename = listFiles.SelectedValue.ToString().Trim();
                string fullFilename = serverDirectory + "//" + filename;

                Uri uri = new Uri(baseUri, fullFilename);

                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
                request.Credentials = new NetworkCredential(textUsername.Text, textPassword.Text);
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                request.UseBinary = checkBoxBinary.Checked;

                response = (FtpWebResponse)request.GetResponse();

                inStream = response.GetResponseStream();

                saveFileDialog1.FileName = filename;

                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    outStream = File.OpenWrite(saveFileDialog1.FileName);
                    byte[] buffer = new byte[1024];
                    int size = 0;
                    while ((size = inStream.Read(buffer, 0, 1024)) > 0)
                    {
                        outStream.Write(buffer, 0, size);
                    }
                    MessageBox.Show("文件下载完毕!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (inStream != null)
                    inStream.Close();
                if (outStream != null)
                    outStream.Close();
                if (response != null)
                    response.Close();
            }
        }
    }
}

  评论这张
 
阅读(928)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018