📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace Ftp客户端
{
public partial class Form1 : Form
{
private TcpClient client;//用于传输控制的连接
private TcpClient DataClient;//用于传输数据的连接
private NetworkStream netStream;
private StreamReader sr;
private StreamWriter sw;
public Form1()
{
InitializeComponent();
this.buttonUpDir.Enabled = false;
}
private void ButtonConnect_Click(object sender, EventArgs e)
{
try
{
client = new TcpClient();
client.Connect("192.168.0.16", 8888);//建立连接
}
catch
{
MessageBox.Show("Connect fault!!");
return;
}
netStream = client.GetStream();
sr = new StreamReader(netStream, System.Text.Encoding.Unicode);
string str = sr.ReadLine();
this.listBox3.Items.Add("Recieve:" + str);
sw = new StreamWriter(netStream, System.Text.Encoding.Unicode);
GetDirAndFiles(@"server:\");
}
private void ButtonDisConnect_Click(object sender, EventArgs e)
{
sw.WriteLine("QUIT");
sw.Flush();
this.listBox3.Items.Add("Send:QUIT");
client.Close();
}
private void buttonUpDir_Click(object sender, EventArgs e)
{
string path = this.label1.Text;
path = path.Substring(0, path.LastIndexOf("\\"));
int num = path.LastIndexOf("\\");
path = path.Substring(0, num + 1);
GetDirAndFiles(path);
}
private void buttonDownload_Click(object sender, EventArgs e)
{
string path = this.listBox2.SelectedItem.ToString();
int num = path.LastIndexOf("\\");
string filename = path.Substring(num + 1, path.Length - num - 1);
string filepath = "\\Temp\\" + filename;
sw.WriteLine("RETR|" + path);
sw.Flush();
this.listBox3.Items.Add("Send:RETR" + path);
this.listBox3.SelectedIndex = this.listBox3.Items.Count - 1;
string str = sr.ReadLine();
DataClient = new TcpClient();
DataClient.Connect("192.168.0.16", 8887);
NetworkStream DataStream = DataClient.GetStream();
str = sr.ReadLine();
this.listBox3.Items.Add("Recieve:" + str);
this.listBox3.SelectedIndex = this.listBox3.Items.Count - 1;
if (str == "150")
{
string str1 = sr.ReadLine();
this.listBox3.Items.Add("FileLength:" + str1 + "Bytes");
this.listBox3.SelectedIndex = this.listBox3.Items.Count - 1;
int length = Convert.ToInt32(str1);
this.progressBar1.Minimum = 0;
this.progressBar1.Maximum = length;
FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write);
int bylength = 2048;
byte[] bye = new byte[2048];
int size = 0;
int i = size;
while ((size = DataStream.Read(bye, 0, bylength)) != 0)
{
i += size;
fs.Write(bye, 0, size);
fs.Flush();
this.progressBar1.Value = i;
bye = new byte[2048];
//this.listBox3.Items.Add("Received FileLength:" + size + "Bytes");
//this.listBox3.SelectedIndex = this.listBox3.Items.Count - 1;
if (i >= length)
break;
}
fs.Close();
DataStream.Close();
DataClient.Close();
if (i != length)
{
MessageBox.Show("DownLoad NOT Complete!");
}
else
{
MessageBox.Show("DownLoad Complete!");
}
this.listBox3.Items.Add("Received FileLength:" + i + "Bytes");
this.listBox3.SelectedIndex = this.listBox3.Items.Count - 1;
this.progressBar1.Value = 0;
}
}
private void GetDirAndFiles(string path)
{
if (this.listBox1.SelectedIndex > -1)
{
this.label1.Text = this.listBox1.SelectedItem.ToString();
}
else
{
this.label1.Text = path;
}
if (path == @"server:\")
{
this.buttonUpDir.Enabled = false;
}
else
{
this.buttonUpDir.Enabled = true;
}
sw.WriteLine("LIST|" + path);
sw.Flush();
this.listBox3.Items.Add("Send:LIST" + path);
this.listBox3.SelectedIndex = this.listBox3.Items.Count - 1;
string str = sr.ReadLine();
this.listBox3.Items.Add("Receive:" + str);
this.listBox3.Items.Add("Send:ensure");
str = sr.ReadLine();
this.listBox3.Items.Add("Receive:" + str);
this.listBox3.SelectedIndex = this.listBox3.Items.Count - 1;
if (str == "125")
{
this.listBox1.Items.Clear();
str = sr.ReadLine();
this.listBox3.Items.Add("Receive:" + str);
this.listBox3.SelectedIndex = this.listBox3.Items.Count - 1;
string[] strarray;
if (str.Length > 0)
{
strarray = str.Split('|');
for (int i = 0; i < strarray.Length; i++)
{
this.listBox1.Items.Add(strarray[i] + "\\");
}
}
this.listBox2.Items.Clear();
str = sr.ReadLine();
this.listBox3.Items.Add("Receive:" + str);
this.listBox3.SelectedIndex = this.listBox3.Items.Count - 1;
if (str.Length > 0)
{
strarray = str.Split('|');
for (int i = 0; i < strarray.Length; i++)
{
this.listBox2.Items.Add(strarray[i]);
}
}
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.listBox1.SelectedIndex == -1)
{
GetDirAndFiles(this.label1.Text);
}
else
{
GetDirAndFiles(this.listBox1.SelectedItem.ToString());
}
}
/// <summary>
/// 上传
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
string filename = this.listBox4.SelectedItem.ToString();
string path = "\\Temp\\" + filename;
sw.WriteLine("STOR|" + filename);
sw.Flush();
this.listBox3.Items.Add("Send:STOR" + filename);
this.listBox3.SelectedIndex = this.listBox3.Items.Count - 1;
string str = sr.ReadLine();
DataClient = new TcpClient();
DataClient.Connect("192.168.0.16", 8887);
NetworkStream DataStream = DataClient.GetStream();
str = sr.ReadLine();
this.listBox3.Items.Add("Recieve:" + str);
this.listBox3.SelectedIndex = this.listBox3.Items.Count - 1;
if (str == "150")
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
sw.WriteLine(fs.Length.ToString());
sw.Flush();
string str1 = fs.Length.ToString();
int length = Convert.ToInt32(str1);
this.progressBar1.Minimum = 0;
this.progressBar1.Maximum = length;
byte[] buffer = new byte[2048];
int number = 0;
//string info;
int i = number;
fs.Seek(0, SeekOrigin.Begin);
while ((number = fs.Read(buffer, 0, 2048)) != 0)
{
i += number;
DataStream.Write(buffer, 0, number);
DataStream.Flush();
this.progressBar1.Value = i;
//buffer = new byte[2048];
}
fs.Close();
//byte[] data = File.ReadAllBytes(parameter);
//netStream.Write(data, 0, data.Length);
DataStream.Close();
DataClient.Close();
if (i != length)
{
MessageBox.Show("UpLoad NOT Complete!");
}
else
{
MessageBox.Show("UpLoad Complete!");
}
this.listBox3.Items.Add("Send FileLength:" + i + "Bytes");
this.listBox3.SelectedIndex = this.listBox3.Items.Count - 1;
this.progressBar1.Value = 0;
}
}
private void button2_Click(object sender, EventArgs e)
{
this.listBox4.Items.Clear();
string path = "\\Temp\\";
String[] dir = Directory.GetFiles(path);
for (int i = 0; i < dir.Length; i++)
{
int num = dir[i].LastIndexOf("\\");
string filename = dir[i].Substring(num + 1, dir[i].Length - num - 1);
this.listBox4.Items.Add(filename);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -