基本信息
源码名称:C# FTP文件上传下载实例源码下载
源码大小:0.13M
文件格式:.zip
开发语言:C#
更新时间:2015-05-07
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 5 元×
微信扫码支付:5 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.IO; using System.Threading; namespace WindowsFormsApplication1 { public partial class Form1 : Form { private bool control = false; private TcpClient client; private int i; private NetworkStream netStream; private FileStream fileStream = null; private Stream stream = null; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int port = 0; IPAddress myIP = IPAddress.Parse("127.0.0.1"); try { myIP = IPAddress.Parse(textBox1.Text); } catch { MessageBox.Show("你输入的IP地址格式错误!"); } try { port = Int32.Parse(textBox2.Text); } catch { MessageBox.Show("请输入整数。"); } try { client.Connect(myIP, port); statusStrip1.Text = "与服务器建立连接"; netStream = client.GetStream(); byte[] b = new byte[6400]; int i = netStream.Read(b, 0, 6400); string str = System.Text.Encoding.BigEndianUnicode.GetString(b); richTextBox1.AppendText(str); int j = richTextBox1.Lines.Length; for(int k=0;k<j-1;k ) { comboBox1.Items.Add(richTextBox1.Lines[k]); } comboBox1.Text = comboBox1.Items[0].ToString(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void button2_Click(object sender, EventArgs e) { control = false; if(saveFileDialog1.ShowDialog() ==DialogResult.OK) {//创建新的文件流 fileStream =new FileStream(saveFileDialog1.FileName,FileMode.Open,FileAccess.Write); //获得服务器网络流 netStream = client.GetStream(); string down = comboBox1.Text "\r\n"; byte[] b = System.Text.Encoding.BigEndianUnicode.GetBytes(down.ToCharArray()); //发送要下载的文件名给服务器 netStream.Write(b, 0, b.Length); //流shuaxin netStream.Flush(); //启动接收文件的线程 Thread thread = new Thread(new ThreadStart(download)); thread.Start(); } } private void download() { //由于在线程中要使用控件,在这里禁止检查跨线程的调用是否合法 Control.CheckForIllegalCrossThreadCalls = false; Stream stream = null; //获取网络流 stream = client.GetStream(); int length = 1024; byte[] b = new byte[1024]; int num = stream.Read(b, 0, length); //读取网络流并写入文件 while(num > 0) { //读取服务器流 string str = System.Text.Encoding.ASCII.GetString(b); int m = str.IndexOf("<EOF>"); if(m == -1) { fileStream.Write(b, 0, num); fileStream.Flush(); statusStrip1.Text = "正在下载文件"; } else { fileStream.Write(b, 0, m); fileStream.Flush(); break; } } fileStream.Close(); MessageBox.Show("下载完毕!"); statusStrip1.Text = "文件下载完毕"; } private void button3_Click(object sender, EventArgs e) { try { netStream = client.GetStream(); string end = "@@@@@@" "\r\n"; byte[] byt = System.Text.Encoding.BigEndianUnicode.GetBytes(end.ToCharArray()); netStream.Write(byt,0,byt.Length); netStream.Flush(); client.Close(); statusStrip1.Text = "与服务器断开连接"; } catch{} } } }