免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3399 | 回复: 0
打印 上一主题 下一主题

C# 使用 FtpWebResponse 编写GUI 简易 FTP客户端 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-20 02:00 |只看该作者 |倒序浏览
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7. using System.Net;
  8. using System.IO;

  9. class FtpClientForm : Form {
  10.     public FtpClientForm() {
  11.         InitializeComponent();
  12.     }

  13.     private string serverDirectory;

  14.     private void OnOpen(object sender, EventArgs e) {
  15.         Cursor currentCursor = this.Cursor;
  16.         FtpWebResponse response = null;
  17.         Stream stream = null;
  18.         this.Cursor = Cursors.WaitCursor;

  19.         FtpWebRequest request = (FtpWebRequest)WebRequest.Create(textServer.Text);
  20.         request.Credentials = new NetworkCredential(textUsername.Text,
  21.               textPassword.Text);
  22.         request.Method = WebRequestMethods.Ftp.ListDirectory;

  23.         response = (FtpWebResponse)request.GetResponse();

  24.         stream = response.GetResponseStream();
  25.         FillDirectoryList(stream);

  26.         serverDirectory = null;
  27.         buttonOpenDirectory.Enabled = false;
  28.         buttonGetFile.Enabled = false;

  29.         if (response != null)
  30.             response.Close();
  31.         if (stream != null)
  32.             stream.Close();
  33.         this.Cursor = currentCursor;
  34.     }

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

  42.     private void OnOpenDirectory(object sender, EventArgs e) {
  43.         FtpWebResponse response = null;
  44.         Stream stream = null;
  45.         string subDirectory = listFiles.SelectedValue.ToString().Trim();
  46.         serverDirectory += @"/" + subDirectory;
  47.         Uri baseUri = new Uri(textServer.Text);
  48.         Uri uri = new Uri(baseUri, serverDirectory);

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

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

  54.         stream = response.GetResponseStream();
  55.         FillDirectoryList(stream);
  56.         if (response != null)
  57.             response.Close();
  58.         if (stream != null)
  59.             stream.Close();
  60.     }

  61.     private void OnDownloadFile(object sender, EventArgs e) {
  62.         FtpWebResponse response = null;
  63.         Stream inStream = null;
  64.         Stream outStream = null;
  65.         Uri baseUri = new Uri(textServer.Text);

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

  68.         Uri uri = new Uri(baseUri, fullFilename);

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

  73.         response = (FtpWebResponse)request.GetResponse();

  74.         inStream = response.GetResponseStream();

  75.         saveFileDialog1.FileName = filename;

  76.         if (saveFileDialog1.ShowDialog() == DialogResult.OK) {
  77.             outStream = File.OpenWrite(saveFileDialog1.FileName);
  78.             byte[] buffer = new byte[4096];
  79.             int size = 0;
  80.             while ((size = inStream.Read(buffer, 0, 4096)) > 0) {
  81.                 outStream.Write(buffer, 0, size);
  82.             }
  83.         }

  84.         if (inStream != null)
  85.             inStream.Close();
  86.         if (outStream != null)
  87.             outStream.Close();
  88.         if (response != null)
  89.             response.Close();

  90.     }

  91.     private void OnFileSelection(object sender, EventArgs e) {
  92.         this.buttonGetFile.Enabled = true;
  93.         this.buttonOpenDirectory.Enabled = true;
  94.     }
  95.     private void InitializeComponent() {
  96.         this.label1 = new System.Windows.Forms.Label();
  97.         this.textServer = new System.Windows.Forms.TextBox();
  98.         this.buttonOpen = new System.Windows.Forms.Button();
  99.         this.statusStrip1 = new System.Windows.Forms.StatusStrip();
  100.         this.listFiles = new System.Windows.Forms.ListBox();
  101.         this.buttonOpenDirectory = new System.Windows.Forms.Button();
  102.         this.buttonGetFile = new System.Windows.Forms.Button();
  103.         this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
  104.         this.checkBoxBinary = new System.Windows.Forms.CheckBox();
  105.         this.label2 = new System.Windows.Forms.Label();
  106.         this.label3 = new System.Windows.Forms.Label();
  107.         this.textUsername = new System.Windows.Forms.TextBox();
  108.         this.textPassword = new System.Windows.Forms.TextBox();
  109.         this.SuspendLayout();
  110.         this.label1.AutoSize = true;
  111.         this.label1.Location = new System.Drawing.Point(28, 31);
  112.         this.label1.Size = new System.Drawing.Size(37, 13);
  113.         this.label1.Text = "Server:";
  114.         this.textServer.Location = new System.Drawing.Point(109, 31);
  115.         this.textServer.Size = new System.Drawing.Size(146, 20);
  116.         this.textServer.Text = "ftp://";
  117.         this.buttonOpen.Location = new System.Drawing.Point(371, 31);
  118.         this.buttonOpen.Size = new System.Drawing.Size(103, 23);
  119.         this.buttonOpen.Text = "Open";
  120.         this.buttonOpen.Click += new System.EventHandler(this.OnOpen);
  121.         this.statusStrip1.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Table;
  122.         this.statusStrip1.Location = new System.Drawing.Point(0, 0);
  123.         this.statusStrip1.Size = new System.Drawing.Size(496, 18);
  124.         this.statusStrip1.Text = "statusStrip1";
  125.         this.listFiles.FormattingEnabled = true;
  126.         this.listFiles.Location = new System.Drawing.Point(28, 149);
  127.         this.listFiles.Size = new System.Drawing.Size(315, 160);
  128.         this.listFiles.SelectedIndexChanged += new System.EventHandler(this.OnFileSelection);
  129.         this.buttonOpenDirectory.Enabled = false;
  130.         this.buttonOpenDirectory.Location = new System.Drawing.Point(371, 77);
  131.         this.buttonOpenDirectory.Name = "buttonOpenDirectory";
  132.         this.buttonOpenDirectory.Size = new System.Drawing.Size(103, 23);
  133.         this.buttonOpenDirectory.TabIndex = 8;
  134.         this.buttonOpenDirectory.Text = "Open Directory";
  135.         this.buttonOpenDirectory.Click += new System.EventHandler(this.OnOpenDirectory);
  136.         this.buttonGetFile.Enabled = false;
  137.         this.buttonGetFile.Location = new System.Drawing.Point(371, 122);
  138.         this.buttonGetFile.Size = new System.Drawing.Size(103, 23);
  139.         this.buttonGetFile.Text = "Get File";
  140.         this.buttonGetFile.Click += new System.EventHandler(this.OnDownloadFile);
  141.         this.checkBoxBinary.AutoSize = true;
  142.         this.checkBoxBinary.Checked = true;
  143.         this.checkBoxBinary.CheckState = System.Windows.Forms.CheckState.Checked;
  144.         this.checkBoxBinary.Location = new System.Drawing.Point(371, 190);
  145.         this.checkBoxBinary.Size = new System.Drawing.Size(81, 17);
  146.         this.checkBoxBinary.Text = "Binary Mode";
  147.         this.label2.AutoSize = true;
  148.         this.label2.Location = new System.Drawing.Point(28, 62);
  149.         this.label2.Size = new System.Drawing.Size(54, 13);
  150.         this.label2.Text = "Username:";
  151.         this.label3.AutoSize = true;
  152.         this.label3.Location = new System.Drawing.Point(28, 101);
  153.         this.label3.Size = new System.Drawing.Size(52, 13);
  154.         this.label3.Text = "Password:";
  155.         this.textUsername.Location = new System.Drawing.Point(109, 62);
  156.         this.textUsername.Size = new System.Drawing.Size(146, 20);
  157.         this.textUsername.Text = "Anonymous";
  158.         this.textPassword.Location = new System.Drawing.Point(109, 101);
  159.         this.textPassword.PasswordChar = '?';
  160.         this.textPassword.Size = new System.Drawing.Size(146, 20);
  161.         this.textPassword.UseSystemPasswordChar = true;
  162.         this.ClientSize = new System.Drawing.Size(496, 353);
  163.         this.Controls.Add(this.textPassword);
  164.         this.Controls.Add(this.textUsername);
  165.         this.Controls.Add(this.label3);
  166.         this.Controls.Add(this.label2);
  167.         this.Controls.Add(this.checkBoxBinary);
  168.         this.Controls.Add(this.buttonGetFile);
  169.         this.Controls.Add(this.buttonOpenDirectory);
  170.         this.Controls.Add(this.listFiles);
  171.         this.Controls.Add(this.buttonOpen);
  172.         this.Controls.Add(this.textServer);
  173.         this.Controls.Add(this.label1);
  174.         this.Text = "FTP Client";
  175.         this.ResumeLayout(false);
  176.         this.PerformLayout();

  177.     }
  178.     private System.Windows.Forms.Label label1;
  179.     private System.Windows.Forms.TextBox textServer;
  180.     private System.Windows.Forms.Button buttonOpen;
  181.     private System.Windows.Forms.StatusStrip statusStrip1;
  182.     private System.Windows.Forms.ListBox listFiles;
  183.     private System.Windows.Forms.Button buttonOpenDirectory;
  184.     private System.Windows.Forms.Button buttonGetFile;
  185.     private System.Windows.Forms.SaveFileDialog saveFileDialog1;
  186.     private System.Windows.Forms.CheckBox checkBoxBinary;
  187.     private System.Windows.Forms.Label label2;
  188.     private System.Windows.Forms.Label label3;
  189.     private System.Windows.Forms.TextBox textUsername;
  190.     private System.Windows.Forms.TextBox textPassword;
  191.     [STAThread]
  192.     static void Main() {
  193.         Application.EnableVisualStyles();
  194.         Application.Run(new FtpClientForm());
  195.     }
  196. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP