C#通过FtpWebResponse 编写GUI 简易 FTP客户端

C#通过FtpWebResponse 编写GUI 简易 FTP客户端,第1张

概述C#通过FtpWebResponse 编写GUI 简易 FTP客户端

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.windows.Forms;using System.Net;using System.IO;  class FtpClIEntForm : Form {    public FtpClIEntForm() {        InitializeComponent();    }      private string serverDirectory;      private voID Onopen(object sender,EventArgs e) {        Cursor currentCursor = this.Cursor;        FtpWebResponse response = null;        Stream stream = null;        this.Cursor = Cursors.WaitCursor;          FtpWebRequest request = (FtpWebRequest)WebRequest.Create(textServer.Text);        request.Credentials = new NetworkCredential(textUsername.Text,textPassword.Text);        request.Method = WebRequestMethods.Ftp.ListDirectory;          response = (FtpWebResponse)request.GetResponse();          stream = response.GetResponseStream();        FillDirectoryList(stream);          serverDirectory = null;        buttonOpenDirectory.Enabled = false;        buttonGetfile.Enabled = false;          if (response != null)            response.Close();        if (stream != null)            stream.Close();        this.Cursor = currentCursor;    }      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 OnopenDirectory(object sender,EventArgs e) {        FtpWebResponse response = null;        Stream stream = null;        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);        if (response != null)            response.Close();        if (stream != null)            stream.Close();    }      private voID OnDownloadfile(object sender,EventArgs e) {        FtpWebResponse response = null;        Stream inStream = null;        Stream outStream = null;        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[4096];            int size = 0;            while ((size = inStream.Read(buffer,4096)) > 0) {                outStream.Write(buffer,size);            }        }          if (inStream != null)            inStream.Close();        if (outStream != null)            outStream.Close();        if (response != null)            response.Close();      }      private voID OnfileSelection(object sender,EventArgs e) {        this.buttonGetfile.Enabled = true;        this.buttonOpenDirectory.Enabled = true;    }    private voID InitializeComponent() {        this.label1 = new System.windows.Forms.Label();        this.textServer = new System.windows.Forms.TextBox();        this.buttonOpen = new System.windows.Forms.button();        this.statusstrip1 = new System.windows.Forms.Statusstrip();        this.Listfiles = new System.windows.Forms.ListBox();        this.buttonOpenDirectory = new System.windows.Forms.button();        this.buttonGetfile = new System.windows.Forms.button();        this.savefileDialog1 = new System.windows.Forms.SavefileDialog();        this.checkBoxBinary = new System.windows.Forms.CheckBox();        this.label2 = new System.windows.Forms.Label();        this.label3 = new System.windows.Forms.Label();        this.textUsername = new System.windows.Forms.TextBox();        this.textPassword = new System.windows.Forms.TextBox();        this.SuspendLayout();        this.label1.autoSize = true;        this.label1.Location = new System.Drawing.Point(28,31);        this.label1.Size = new System.Drawing.Size(37,13);        this.label1.Text = "Server:";        this.textServer.Location = new System.Drawing.Point(109,31);        this.textServer.Size = new System.Drawing.Size(146,20);        this.textServer.Text = "ftp://";        this.buttonOpen.Location = new System.Drawing.Point(371,31);        this.buttonOpen.Size = new System.Drawing.Size(103,23);        this.buttonOpen.Text = "Open";        this.buttonOpen.Click += new System.EventHandler(this.Onopen);        this.statusstrip1.LayoutStyle = System.windows.Forms.ToolStripLayoutStyle.table;        this.statusstrip1.Location = new System.Drawing.Point(0,0);        this.statusstrip1.Size = new System.Drawing.Size(496,18);        this.statusstrip1.Text = "statusstrip1";        this.Listfiles.FormattingEnabled = true;        this.Listfiles.Location = new System.Drawing.Point(28,149);        this.Listfiles.Size = new System.Drawing.Size(315,160);        this.Listfiles.SelectedindexChanged += new System.EventHandler(this.OnfileSelection);        this.buttonOpenDirectory.Enabled = false;        this.buttonOpenDirectory.Location = new System.Drawing.Point(371,77);        this.buttonOpenDirectory.name = "buttonOpenDirectory";        this.buttonOpenDirectory.Size = new System.Drawing.Size(103,23);        this.buttonOpenDirectory.TabIndex = 8;        this.buttonOpenDirectory.Text = "Open Directory";        this.buttonOpenDirectory.Click += new System.EventHandler(this.OnopenDirectory);        this.buttonGetfile.Enabled = false;        this.buttonGetfile.Location = new System.Drawing.Point(371,122);        this.buttonGetfile.Size = new System.Drawing.Size(103,23);        this.buttonGetfile.Text = "Get file";        this.buttonGetfile.Click += new System.EventHandler(this.OnDownloadfile);        this.checkBoxBinary.autoSize = true;        this.checkBoxBinary.Checked = true;        this.checkBoxBinary.CheckState = System.windows.Forms.CheckState.Checked;        this.checkBoxBinary.Location = new System.Drawing.Point(371,190);        this.checkBoxBinary.Size = new System.Drawing.Size(81,17);        this.checkBoxBinary.Text = "Binary Mode";        this.label2.autoSize = true;        this.label2.Location = new System.Drawing.Point(28,62);        this.label2.Size = new System.Drawing.Size(54,13);        this.label2.Text = "Username:";        this.label3.autoSize = true;        this.label3.Location = new System.Drawing.Point(28,101);        this.label3.Size = new System.Drawing.Size(52,13);        this.label3.Text = "Password:";        this.textUsername.Location = new System.Drawing.Point(109,62);        this.textUsername.Size = new System.Drawing.Size(146,20);        this.textUsername.Text = "Anonymous";        this.textPassword.Location = new System.Drawing.Point(109,101);        this.textPassword.PasswordChar = '?';        this.textPassword.Size = new System.Drawing.Size(146,20);        this.textPassword.UseSystemPasswordChar = true;        this.ClIEntSize = new System.Drawing.Size(496,353);        this.Controls.Add(this.textPassword);        this.Controls.Add(this.textUsername);        this.Controls.Add(this.label3);        this.Controls.Add(this.label2);        this.Controls.Add(this.checkBoxBinary);        this.Controls.Add(this.buttonGetfile);        this.Controls.Add(this.buttonOpenDirectory);        this.Controls.Add(this.Listfiles);        this.Controls.Add(this.buttonOpen);        this.Controls.Add(this.textServer);        this.Controls.Add(this.label1);        this.Text = "FTP ClIEnt";        this.ResumeLayout(false);        this.Performlayout();      }    private System.windows.Forms.Label label1;    private System.windows.Forms.TextBox textServer;    private System.windows.Forms.button buttonOpen;    private System.windows.Forms.Statusstrip statusstrip1;    private System.windows.Forms.ListBox Listfiles;    private System.windows.Forms.button buttonOpenDirectory;    private System.windows.Forms.button buttonGetfile;    private System.windows.Forms.SavefileDialog savefileDialog1;    private System.windows.Forms.CheckBox checkBoxBinary;    private System.windows.Forms.Label label2;    private System.windows.Forms.Label label3;    private System.windows.Forms.TextBox textUsername;    private System.windows.Forms.TextBox textPassword;    [STAThread]    static voID Main() {        Application.EnableVisualStyles();        Application.Run(new FtpClIEntForm());    }}//该片段来自于http://www.codesnippet.cn/detail/2303201511957.@R_419_6832@

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的C#通过FtpWebResponse 编写GUI 简易 FTP客户端全部内容,希望文章能够帮你解决C#通过FtpWebResponse 编写GUI 简易 FTP客户端所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1238077.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存