前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >C#通过FtpWebResponse 编写GUI 简易 FTP客户端

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

原创
作者头像
用户7705674
修改于 2021-11-03 01:48:01
修改于 2021-11-03 01:48:01
88000
代码可运行
举报
文章被收录于专栏:css小迷妹css小迷妹
运行总次数:0
代码可运行
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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, 0, 4096)) > 0) {
            outStream.Write(buffer, 0, 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());
}

}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
解决msmq接收远程主机私有队列消息的问题!
通过调用windows\system32\mqoa.dll 这个Message Queuing ActiveX Interface API函数就可以做到; 下面是一个简单的例子供参考; using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using MSMQ; namespace MyTest {     /// <summ
阿新
2018/04/13
2.4K0
C# 使用QRCoder生成二维码
最近瞎琢磨的一些小东西,也算是一个比较完整的二维码生成了,上手也很快,可自行扩展。 现在生成二维码有多种方式,我使用的是QRCoder。
郑子铭
2024/04/15
4740
C# 使用QRCoder生成二维码
C#-TextBox-登录窗口密码不可见—ShinePans[通俗易懂]
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/161502.html原文链接:https://javaforall.cn
全栈程序员站长
2022/09/09
1.4K0
C#-TextBox-登录窗口密码不可见—ShinePans[通俗易懂]
c#基于Tablet pc实现的手写输入
需要安装Tablet pc,win7的话 直接在控制面板》程序和应用》添加组建里面勾选上添加
冰封一夏
2019/09/11
1.2K0
在树莓派用C#+Winform实现传感器监测
Raspberry Pi 3B+ 树莓派GPIO扩展板 3.5寸电容触摸屏(GPIO接口) 土壤湿度传感器(GPIO接口) 光照传感器(GPIO接口) 由于作品已经交上去了 这里只能先放个以前的图
沙漠尽头的狼
2021/12/01
1K0
在树莓派用C#+Winform实现传感器监测
Using sqlite with .NET
The other day I found that there is a .NET wrapper for sqlite. sqlite is a very cool embeddable SQL-92 database engine. It's a single library that gives you a very fast, very scalable (2TB), single file, multi-user database. I thought the .NET wrapper is e
张善友
2018/01/31
6570
windows计算器
usingSystem;usingSystem.Drawing;usingSystem.Win运维
Java架构师必看
2021/03/22
1.4K0
C#学习系列文章之Windows窗体应用程序003
紧接着上一篇文章,我已经学习了控制台的使用,以及创建不同应用的Helloworld程序,这一篇文章,我介绍Windows窗体应用程序的简单实用,按照调用流程一步一步的操作,希望能帮助初学者也能一步一步的搭建起自己的第一个应用。由于本人水平有限,文章中难免有介绍不足的地方,敬请谅解。
算法发
2020/01/16
2K0
C#学习系列文章之Windows窗体应用程序003
(十四)c#Winform自定义控件-键盘(一)
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/11
1.8K0
(十四)c#Winform自定义控件-键盘(一)
动态获取当前屏幕中光标所在位置的颜色
usingSystem;usingSystem.Drawing;usingSystem.C运维
Java架构师必看
2020/10/15
2.9K0
(二十四)c#Winform自定义控件-单标题窗体
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/11
4840
(二十四)c#Winform自定义控件-单标题窗体
(十八)c#Winform自定义控件-提示框
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/11
1.2K0
(十八)c#Winform自定义控件-提示框
(二十)c#Winform自定义控件-有后退的窗体
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/11
4520
(二十)c#Winform自定义控件-有后退的窗体
c#QQ连连看辅助
游戏辅助有三种方法,一种是读内存,这个不知道怎么分析,还有一种是获取封包,这个分析起来复杂,最后一种是图片识别再分析,这里采用最后一种 图片识别来做。
冰封一夏
2019/09/11
6530
【愚公系列】2023年11月 Winform控件专题 RadioButton控件详解
Winform控件是Windows Forms中的用户界面元素,它们可以用于创建Windows应用程序的各种视觉和交互组件,例如按钮、标签、文本框、下拉列表框、复选框、单选框、进度条等。开发人员可以使用Winform控件来构建用户界面并响应用户的操作行为,从而创建功能强大的桌面应用程序。
愚公搬代码
2023/11/24
3660
(二十三)c#Winform自定义控件-等待窗体
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/11
1K0
(二十三)c#Winform自定义控件-等待窗体
(三十三)c#Winform自定义控件-日期控件
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/11
5K0
(三十三)c#Winform自定义控件-日期控件
(十)c#Winform自定义控件-横向列表
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/11
2K0
(十)c#Winform自定义控件-横向列表
(三十七)c#Winform自定义控件-有标题的面板
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/11
6960
(三十七)c#Winform自定义控件-有标题的面板
(二)c#Winform自定义控件-按钮
GitHub:https://github.com/kwwwvagaa/NetWinformControl
冰封一夏
2019/09/11
1.5K0
(二)c#Winform自定义控件-按钮
推荐阅读
相关推荐
解决msmq接收远程主机私有队列消息的问题!
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档